程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPCo

GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPCo

編輯:Delphi

 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 使用的, 下面是一些簡單的例子.

  比例設置:

GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPColorMatrix)變換

  查看原圖(大圖)

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; 

增減量:

GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPColorMatrix)變換

  查看原圖(大圖)

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; 

紅色使用其他顏色的比例:

GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPColorMatrix)變換

  查看原圖(大圖)

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; 

綠色使用其他顏色的比例:

GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPColorMatrix)變換

  查看原圖(大圖)

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; 

 藍色使用其他顏色的比例:

GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPColorMatrix)變換

  查看原圖(大圖)

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; 


 查看獨立的顏色通道:

GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPColorMatrix)變換

  查看原圖(大圖)

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; 

 轉灰度:

GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPColorMatrix)變換

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; 

 加亮、變暗:

GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPColorMatrix)變換

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; 

 顏色旋轉:

GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPColorMatrix)變換

  查看原圖(大圖)

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; 

 對比度:

GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPColorMatrix)變換

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; 

 反色:

GdiPlus[59]: 圖像(十一) IGPImageAttributes 之顏色矩陣(TGPColorMatrix)變換

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; 












  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved