随笔 - 2146  文章 - 97 评论 - 11761 trackbacks - 253


IGPMatrix 矩阵 是个接口, 要通过 TGPMatrix 实例化后使用, 其内置了很多方法和属性.

TGPColorMatrix 只是一个结构体, 除了矩阵数据(5*5)外, 它只有一个方法: TGPColorMatrix.SetToIdentity.

通过 SetToIdentity 方法可初始化矩阵, 初始化后的数据是:

┏             ┓
┃1  0  0  0  0┃
┃0  1  0  0  0┃
┃0  0  1  0  0┃
┃0  0  0  1  0┃
┃0  0  0  0  1┃
┗             ┛

对角线上的 1 是比例; 应用这个数据后, 目标不会有任何变化.

其中的第 5 行和第 5 列用于辅助运算, 我们主要操作 4*4 的范围; 为便于理解可以这样表示:

┏              ┓
┃rr  gr  br  ar┃
┃rg  gg  bg  ag┃
┃rb  gb  bb  ab┃
┃ra  ga  ba  aa┃
┗              ┛

rr、gg、bb、aa 分别表示红、绿、蓝和透明度的比例; 譬如 aa = 0.5 表示半透明.

第四行的 ra、ga、ba 分别是颜色的增减量; 譬如 ra = 0.1 表示红色增加 10%.

第一列的 rr、rg、rb 分别表示: 红色应用其他颜色的比例; 譬如 rg = 0.5, 那么红色的值将是绿色成分的 50%.

第二列的 gr、gg、gb 分别表示: 绿应用其他颜色的比例.

第三列的 br、bg、bb 分别表示: 蓝色应用其他颜色的比例.

还有一个颜色旋转的概念:

//红色与绿色绕蓝色旋转(其中的 f 是弧度, 弧度 = 角度 * Pi / 180):
┏                       ┓
┃ Cos(f)  Sin(f)  br  ar┃
┃-Sin(f)  Cos(f)  bg  ag┃
┃ rb      gb      bb  ab┃
┃ ra      ga      ba  aa┃
┗                       ┛

//绿色与蓝色绕红色旋转:
┏                       ┓
┃rr   gr      br      ar┃
┃rg   Cos(f)  Sin(f)  ag┃
┃rb  -Sin(f)  Cos(f)  ab┃
┃ra   ga      ba      aa┃
┗                       ┛

//红色与蓝色绕绿色旋转:
┏                       ┓
┃ Cos(f)  gr  Sin(f)  ar┃
┃-Sin(f)  gg  Cos(f)  ag┃
┃ rb      gb  bb      ab┃
┃ ra      ga  ba      aa┃
┗                       ┛


这个东西可千变万化, 一时很难彻底理解, 譬如前人算出的灰度算法:

┏                      ┓
┃0.299  0.299  0.299  0┃
┃0.518  0.518  0.518  0┃
┃0.114  0.114  0.114  0┃
┃0      0      0      1┃
┗                      ┛


颜色矩阵是通过 ImageAttributes 使用的, 下面是一些简单的例子.

比例设置:


uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Img: IGPImage;
  Attr: IGPImageAttributes;
  ColorMatrix: TGPColorMatrix;
  Rect: TGPRectF;
  Brush: IGPHatchBrush;
begin
  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFD0D0D0, $FFFFFFFF);
  Graphics.FillRectangle(Brush, TGPRect.Create(ClientRect));

  Img := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg');
  Rect.Initialize(4, 4, Img.Width * 0.75, Img.Height * 0.75);
  Attr := TGPImageAttributes.Create;

  { 原始图片 }
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);

  { 红色比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 0] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 绿色比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[1, 1] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 蓝色比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[2, 2] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 透明度比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[3, 3] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;


增减量:


uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Img: IGPImage;
  Attr: IGPImageAttributes;
  ColorMatrix: TGPColorMatrix;
  Rect: TGPRectF;
  Brush: IGPHatchBrush;
begin
  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFD0D0D0, $FFFFFFFF);
  Graphics.FillRectangle(Brush, TGPRect.Create(ClientRect));

  Img := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg');
  Rect.Initialize(4, 4, Img.Width * 0.75, Img.Height * 0.75);
  Attr := TGPImageAttributes.Create;

  { 原始图片 }
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);

  { 红色增减 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[4, 0] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 绿色增减 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[4, 1] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 蓝色增减 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[4, 2] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 透明度增减 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[4, 3] := -0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;


红色使用其他颜色的比例:


uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Img: IGPImage;
  Attr: IGPImageAttributes;
  ColorMatrix: TGPColorMatrix;
  Rect: TGPRectF;
  Brush: IGPHatchBrush;
begin
  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFD0D0D0, $FFFFFFFF);
  Graphics.FillRectangle(Brush, TGPRect.Create(ClientRect));

  Img := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg');
  Rect.Initialize(4, 4, Img.Width * 0.75, Img.Height * 0.75);
  Attr := TGPImageAttributes.Create;

  { 原始图片 }
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);

  { 红色应用红色的比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 0] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 红色应用绿色的比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 1] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 红色应用蓝色的比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 2] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;


绿色使用其他颜色的比例:


uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Img: IGPImage;
  Attr: IGPImageAttributes;
  ColorMatrix: TGPColorMatrix;
  Rect: TGPRectF;
  Brush: IGPHatchBrush;
begin
  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFD0D0D0, $FFFFFFFF);
  Graphics.FillRectangle(Brush, TGPRect.Create(ClientRect));

  Img := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg');
  Rect.Initialize(4, 4, Img.Width * 0.75, Img.Height * 0.75);
  Attr := TGPImageAttributes.Create;

  { 原始图片 }
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);

  { 绿色应用红色的比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[1, 0] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 绿色应用绿色的比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[1, 1] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 绿色应用蓝色的比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[1, 2] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;


蓝色使用其他颜色的比例:


uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Img: IGPImage;
  Attr: IGPImageAttributes;
  ColorMatrix: TGPColorMatrix;
  Rect: TGPRectF;
  Brush: IGPHatchBrush;
begin
  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFD0D0D0, $FFFFFFFF);
  Graphics.FillRectangle(Brush, TGPRect.Create(ClientRect));

  Img := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg');
  Rect.Initialize(4, 4, Img.Width * 0.75, Img.Height * 0.75);
  Attr := TGPImageAttributes.Create;

  { 原始图片 }
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);

  { 蓝色应用红色的比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[2, 0] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 蓝色应用绿色的比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[2, 1] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 蓝色应用蓝色的比例 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[2, 2] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;

查看独立的颜色通道:


uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Img: IGPImage;
  Attr: IGPImageAttributes;
  ColorMatrix: TGPColorMatrix;
  Rect: TGPRectF;
  Brush: IGPHatchBrush;
begin
  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFD0D0D0, $FFFFFFFF);
  Graphics.FillRectangle(Brush, TGPRect.Create(ClientRect));

  Img := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg');
  Rect.Initialize(4, 4, Img.Width * 0.75, Img.Height * 0.75);
  Attr := TGPImageAttributes.Create;

  { 原始图片 }
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);

  { 只查看红色通道 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 0] := 1;
  ColorMatrix.M[1, 1] := 0;
  ColorMatrix.M[2, 2] := 0;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 只查看绿色通道 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 0] := 0;
  ColorMatrix.M[1, 1] := 1;
  ColorMatrix.M[2, 2] := 0;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 只查看蓝色通道 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 0] := 0;
  ColorMatrix.M[1, 1] := 0;
  ColorMatrix.M[2, 2] := 1;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 半透明查看红色通道 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 0] := 1;
  ColorMatrix.M[1, 1] := 0;
  ColorMatrix.M[2, 2] := 0;
  ColorMatrix.M[3, 3] := 0.5;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;


转灰度:


uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Img: IGPImage;
  Attr: IGPImageAttributes;
  ColorMatrix: TGPColorMatrix;
  Rect: TGPRectF;
  Brush: IGPHatchBrush;
begin
  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFD0D0D0, $FFFFFFFF);
  Graphics.FillRectangle(Brush, TGPRect.Create(ClientRect));

  Img := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg');
  Rect.Initialize(4, 4, Img.Width * 0.75, Img.Height * 0.75);
  Attr := TGPImageAttributes.Create;

  { 原始图片 }
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);

  { 灰度 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 0] := 0.299;
  ColorMatrix.M[0, 1] := 0.299;
  ColorMatrix.M[0, 2] := 0.299;
  ColorMatrix.M[1, 0] := 0.518;
  ColorMatrix.M[1, 1] := 0.518;
  ColorMatrix.M[1, 2] := 0.518;
  ColorMatrix.M[2, 0] := 0.114;
  ColorMatrix.M[2, 1] := 0.114;
  ColorMatrix.M[2, 2] := 0.114;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;


加亮、变暗:


uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Img: IGPImage;
  Attr: IGPImageAttributes;
  ColorMatrix: TGPColorMatrix;
  Rect: TGPRectF;
  Brush: IGPHatchBrush;
begin
  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFD0D0D0, $FFFFFFFF);
  Graphics.FillRectangle(Brush, TGPRect.Create(ClientRect));

  Img := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg');
  Rect.Initialize(4, 4, Img.Width * 0.75, Img.Height * 0.75);
  Attr := TGPImageAttributes.Create;

  { 原始图片 }
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);

  { 加亮 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[3, 0] := 0.2;
  ColorMatrix.M[3, 1] := 0.2;
  ColorMatrix.M[3, 2] := 0.2;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 变暗 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[3, 0] := -0.2;
  ColorMatrix.M[3, 1] := -0.2;
  ColorMatrix.M[3, 2] := -0.2;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;


颜色旋转:


uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Img: IGPImage;
  Attr: IGPImageAttributes;
  ColorMatrix: TGPColorMatrix;
  Rect: TGPRectF;
  Brush: IGPHatchBrush;
  f: Single;
begin
  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFD0D0D0, $FFFFFFFF);
  Graphics.FillRectangle(Brush, TGPRect.Create(ClientRect));

  Img := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg');
  Rect.Initialize(4, 4, Img.Width * 0.75, Img.Height * 0.75);
  Attr := TGPImageAttributes.Create;
  f := 30 * Pi / 180; { 准备旋转 30 度角 }

  { 原始图片 }
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);

  { 红色与绿色绕蓝色旋转 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 0] :=  Cos(f); ColorMatrix.M[0, 1] := Sin(f);
  ColorMatrix.M[1, 0] := -Sin(f); ColorMatrix.M[1, 1] := Cos(f);
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 绿色与蓝色绕红色旋转 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[1, 1] :=  Cos(f); ColorMatrix.M[1, 2] := Sin(f);
  ColorMatrix.M[2, 1] := -Sin(f); ColorMatrix.M[2, 2] := Cos(f);
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);

  { 红色与蓝色绕绿色旋转 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 0] :=  Cos(f); ColorMatrix.M[0, 2] := Sin(f);
  ColorMatrix.M[1, 0] := -Sin(f); ColorMatrix.M[1, 2] := Cos(f);
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;


对比度:


uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Img: IGPImage;
  Attr: IGPImageAttributes;
  ColorMatrix: TGPColorMatrix;
  Rect: TGPRectF;
  Brush: IGPHatchBrush;
begin
  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFD0D0D0, $FFFFFFFF);
  Graphics.FillRectangle(Brush, TGPRect.Create(ClientRect));

  Img := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg');
  Rect.Initialize(10, 10, Img.Width * 0.75, Img.Height * 0.75);
  Attr := TGPImageAttributes.Create;

  { 原始图片 }
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);

  { 对比度 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 0] := 1.1;
  ColorMatrix.M[1, 1] := 1.1;
  ColorMatrix.M[2, 2] := 1.1;
  ColorMatrix.M[3, 0] := 0.001;
  ColorMatrix.M[3, 1] := 0.001;
  ColorMatrix.M[3, 2] := 0.001;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;


反色:


uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Img: IGPImage;
  Attr: IGPImageAttributes;
  ColorMatrix: TGPColorMatrix;
  Rect: TGPRectF;
  Brush: IGPHatchBrush;
begin
  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFD0D0D0, $FFFFFFFF);
  Graphics.FillRectangle(Brush, TGPRect.Create(ClientRect));

  Img := TGPImage.Create('C:\GdiPlusImg\ImageFileSmall.jpg');
  Rect.Initialize(10, 10, Img.Width * 0.75, Img.Height * 0.75);
  Attr := TGPImageAttributes.Create;

  { 原始图片 }
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);

  { 反色(或叫负片、底片)效果 }
  ColorMatrix.SetToIdentity;
  ColorMatrix.M[0, 0] := -1;
  ColorMatrix.M[1, 1] := -1;
  ColorMatrix.M[2, 2] := -1;
  ColorMatrix.M[3, 0] := 0.999;
  ColorMatrix.M[3, 1] := 0.999;
  ColorMatrix.M[3, 2] := 0.999;
  Attr.SetColorMatrix(ColorMatrix);
  Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
  Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;

posted on 2010-01-01 22:52 万一 阅读(...) 评论(...) 编辑 收藏