IGPImageAttributes 的方法:
SetWrapMode() { 設置環繞模式 }
這是 IGPImageAttributes 中出了 Clone 以外唯一個和顏色不相關方法.
SetThreshold()、SetThreshold() { 設置、取消 "阈值" }
取值范圍: 0 ..1
假如設置閥值為 0.5 , 那麼超過 128 的紅色都變為 256 , 少於 128 的紅色都變為 0 ;
綠色、藍色也是如此.
SetRemapTable()、ClearRemapTable()、SetBrushRemapTable()、ClearBrushRemapTable
{ 設置、取消 "顏色映射表" }
就是用另一種顏色替換指定顏色; 其主要參數是個數組, 可以替換多種顏色;
SetBrushRemapTable 和 ClearBrushRemapTable 專用於圖元文件的畫刷;
SetRemapTable 也可以完成 SetBrushRemapTable 的任務, 所以感覺 SetBrushRemapTable 有點多余.
SetColorKey()、ClearColorKey() { 設置、取消透明色范圍 }
如果只指定一種透明色, 可以把兩個參數值設為相同.
SetGamma()、ClearGamma() { 設置、取消伽瑪校正值(或叫灰度校正值) }
可以用它調整亮度; 取值范圍: 0.1..5, 默認 1
SetOutputChannel()、ClearOutputChannel() { 設置、取消 CMYK 輸出通道 }
看到的 CMYK(Cyan、Magenta、Yellow、Black)各通道的效果都是灰度, 其實是顏色的強度.
SetToIdentity()、Reset() { 重置(回復默認值) }
重置時, 實用感覺是重置指定類型最好用 SetToIdentity, 全部重置用 Reset.
SetNoOp()、ClearNoOp() { 禁用、取消 SetNoOp 的禁用(也就是再使用) }
GetAdjustedPalette() { 獲取變換後的調色板 }
即使是在顏色變換後, 從 IGPImage 獲取的調色板也還是原來的;
獲取變更後的調色板需要 IGPImageAttributes.GetAdjustedPalette().
SetColorMatrix()、ClearColorMatrix() { 設置、取消顏色調整矩陣 }
SetColorMatrices()、ClearColorMatrices() { 設置、取消顏色調整矩陣與灰度調整矩陣}
SetColorMatrices 的參數中有兩個矩陣, 前者用於顏色矩陣、後者用於灰度矩陣.
SetColorMatrices 的第三個參數需要個 TGPColorMatrixFlags 類型的枚舉值:
TGPColorMatrixFlags = (
ColorMatrixFlagsDefault : //只使用第一個矩陣調整顏色與灰度, 這和使用 SetColorMatrix 效果相同;
ColorMatrixFlagsSkipGrays: //只調整顏色矩陣, 同樣會忽略第二個矩陣;
ColorMatrixFlagsAltGray : //只調整灰色矩陣, 此時一個矩陣被忽略.
);
SetOutputChannelColorProfile()、ClearOutputChannelColorProfile() { 設置、取消顏色配置文件}
*.icc 或 *.icm; 默認路徑是 ...Windows\system32\spool\drivers\color\
上述方法除 SetWrapMode、SetBrushRemapTable、ClearBrushRemapTable 外,
都有個 TGPColorAdjustType 類型的參數:
TGPColorAdjustType = (
ColorAdjustTypeDefault, // 默認, 適應用各類型
ColorAdjustTypeBitmap, // 用於位圖
ColorAdjustTypeBrush, // 用於圖元文件中的畫刷
ColorAdjustTypePen, // 用於圖元文件中的畫筆
ColorAdjustTypeText, // 用於圖元文件中的文本的畫刷
ColorAdjustTypeCount, // 無用
ColorAdjustTypeAny // 保留
);
//如果是 ColorAdjustTypeDefault, 那麼變換將應用到各類型(位圖、畫筆、畫刷等);
//使用 Default 後, 如果再單獨指定變換, 當然它不會再使用 Default 值, 但 Clear 後就回不到 Default 了.
WrapModeTile 測試:
查看原圖(大圖)
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics: IGPGraphics;
Img: IGPImage;
Attr: IGPImageAttributes;
Rect: TGPRect;
begin
Graphics := TGPGraphics.Create(Handle);
Img := TGPImage.Create('C:\GdiPlusImg\Bird.bmp');
Rect.Initialize(4, 4, Img.Width, Img.Height);
Attr := TGPImageAttributes.Create;
{ 原始 }
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);
{ WrapModeTile }
Attr.SetWrapMode(WrapModeTile);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width*2, Img.Height*2, UnitPixel, Attr);
{ WrapModeTileFlipX }
Attr.SetWrapMode(WrapModeTileFlipX);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width*2, Img.Height*2, UnitPixel, Attr);
{ WrapModeTileFlipY }
Attr.SetWrapMode(WrapModeTileFlipY);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width*2, Img.Height*2, UnitPixel, Attr);
{ WrapModeTileFlipXY }
Attr.SetWrapMode(WrapModeTileFlipXY);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width*2, Img.Height*2, UnitPixel, Attr);
{ WrapModeClamp }
Attr.SetWrapMode(WrapModeClamp);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width*2, Img.Height*2, UnitPixel, Attr);
end;
SetThreshold 測試:
查看原圖(大圖)
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
const
Colors: array[0..6] of Cardinal = ($FFFF0000, $FF00FF00, $FF0000FF,
$FFFFFF00, $FFFF00FF, $FF00FFFF, $FF000000);
var
Graphics, GraphicsImg: IGPGraphics;
Img: IGPImage;
Attr: IGPImageAttributes;
Rect: TGPRect;
i: Integer;
f: Single;
begin
{ 沒有合適的圖片, 先自畫一個顏色漸變圖片 }
Img := TGPBitmap.Create(80, 140);
GraphicsImg := TGPGraphics.Create(Img);
for i := 0 to 6 do
begin
Rect.Initialize(0, i * Img.Height div 7, Img.Width, Img.Height div 7);
GraphicsImg.FillRectangle(
TGPLinearGradIEntBrush.Create(Rect, Colors[i], $FFFFFFFF, 0), Rect);
end;
Graphics := TGPGraphics.Create(Handle);
Graphics.FillRectangle(
TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFD0D0D0, $FF606060),
TGPRect.Create(ClIEntRect));
Rect.Initialize(12, 6, Img.Width, Img.Height);
Attr := TGPImageAttributes.Create;
{ 原始 }
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);
{ SetThreshold }
f := 0;
while f <= 1 do
begin
Attr.SetThreshold(f);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
f := f + 0.25;
end;
end;
SetRemapTable 與 SetBrushRemapTable 測試:
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics, GraphicsImg: IGPGraphics;
Img: IGPImage;
Attr: IGPImageAttributes;
Rect: TGPRectF;
ColorMapArr: array[0..1] of TGPColorMap;
begin
Graphics := TGPGraphics.Create(Handle);
Img := TGPImage.Create('C:\GdiPlusImg\SampleMetafile.emf');
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);
{ SetRemapTable: 把其中的藍色替換為黑色、綠色替換為紅色 }
ColorMapArr[0].OldColor := $FF0000FF;
ColorMapArr[0].NewColor := $FF000000;
ColorMapArr[1].OldColor := $FF00FF00;
ColorMapArr[1].NewColor := $FFFF0000;
Attr.SetRemapTable(ColorMapArr);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
Attr.ClearRemapTable();
Attr.SetBrushRemapTable(ColorMapArr); { 這與下面一行的效果是一樣的 }
// Attr.SetRemapTable(ColorMapArr, ColorAdjustTypeBrush);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
Attr.ClearBrushRemapTable;
end;
SetColorKey 測試:
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics, GraphicsImg: IGPGraphics;
Img: IGPImage;
Attr: IGPImageAttributes;
Rect: TGPRect;
begin
Graphics := TGPGraphics.Create(Handle);
Img := TGPImage.Create('C:\GdiPlusImg\Bird.bmp');
Rect.Initialize(10, 10, Img.Width, Img.Height);
Attr := TGPImageAttributes.Create;
Graphics.FillRectangle(TGPHatchBrush.Create(
HatchStyleDiagonalCross, $FFC0C0C0, $FFE0E0E0), TGPRect.Create(ClIEntRect));
{ 原始 }
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);
//
Attr.SetColorKey($FFFFFFFF, $FFFFFFFF);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
//
Attr.SetColorKey($FF003399, $FF3366CC);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
//
Attr.SetColorKey($FF003399, $FFFFFFFF);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;
SetGamma 測試:
查看原圖(大圖)
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics, GraphicsImg: IGPGraphics;
Img: IGPImage;
Attr: IGPImageAttributes;
Rect: TGPRectF;
f: Single;
begin
Graphics := TGPGraphics.Create(Handle);
Img := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg');
Rect.Initialize(4, 4, Img.Width / 2, Img.Height / 2);
Attr := TGPImageAttributes.Create;
{ 原始 }
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);
f := 0.25;
while f < 3 do
begin
Attr.SetGamma(f);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
f := f + 0.5;
end;
end;
SetOutputChannel 測試:
查看原圖(大圖)
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics, GraphicsImg: IGPGraphics;
Img: IGPImage;
Attr: IGPImageAttributes;
Rect: TGPRect;
i: Integer;
begin
Graphics := TGPGraphics.Create(Handle);
Img := TGPImage.Create('C:\GdiPlusImg\Bird.bmp');
Rect.Initialize(4, 4, Img.Width, Img.Height);
Attr := TGPImageAttributes.Create;
{ 原始 }
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);
for i := 0 to 3 do
begin
Attr.SetOutputChannel(TGPColorChannelFlags(i));
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;
end;
SetToIdentity 測試:
查看原圖(大圖)
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics, GraphicsImg: IGPGraphics;
Img: IGPImage;
Attr: IGPImageAttributes;
Rect: TGPRectF;
ColorMapArr: array[0..1] of TGPColorMap;
begin
Graphics := TGPGraphics.Create(Handle);
Img := TGPImage.Create('C:\GdiPlusImg\SampleMetafile.emf');
Rect.Initialize(4, 4, Img.Width * 0.7, Img.Height * 0.7);
Attr := TGPImageAttributes.Create;
ColorMapArr[0].OldColor := $FF00FF00;
ColorMapArr[0].NewColor := $FFFF0000;
ColorMapArr[1].OldColor := $FF0000FF;
ColorMapArr[1].NewColor := $FF000000;
{ 原始 }
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);
{ 設置顏色替換(默認包括畫筆和畫刷) }
Attr.SetRemapTable(ColorMapArr);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
{ 重置畫筆的變換 }
Attr.SetToIdentity(ColorAdjustTypePen);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
{ 重置畫刷的變換 }
Attr.SetToIdentity(ColorAdjustTypeBrush);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;
SetNoOp、ClearNoOp、Reset 測試:
查看原圖(大圖)
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics, GraphicsImg: IGPGraphics;
Img: IGPImage;
Attr: IGPImageAttributes;
Rect: TGPRectF;
begin
Graphics := TGPGraphics.Create(Handle);
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);
{ 變換 }
Attr.SetGamma(0.3);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
{ 禁用指定的(這裡是默認的)顏色變換 }
Attr.SetNoOp();
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
{ 取消禁用 }
Attr.ClearNoOp();
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
{ 重置 }
Attr.Reset();
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;
GetAdjustedPalette 測試:
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics, GraphicsImg: IGPGraphics;
Img: IGPImage;
Attr: IGPImageAttributes;
Rect: TGPRect;
ColorMapArr: array[0..2] of TGPColorMap;
ColorPalette: IGPColorPalette;
i: Integer;
Brush: IGPSolidBrush;
begin
Img := TGPImage.Create('C:\GdiPlusImg\Stripes.bmp');
if not IsIndexedPixelFormat(Img.PixelFormat) then Exit;
Graphics := TGPGraphics.Create(Handle);
Rect.Initialize(10, 10, Img.Width, Img.Height);
Attr := TGPImageAttributes.Create;
Graphics.FillRectangle(
TGPHatchBrush.Create(HatchStyleDiagonalCross, $FFC0C0C0, $FFE0E0E0),
TGPRect.Create(ClIEntRect));
{ 原始 }
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);
{ SetRemapTable }
ColorMapArr[0].OldColor := $FFFF0000;
ColorMapArr[0].NewColor := $FF800000;
ColorMapArr[1].OldColor := $FF00FF00;
ColorMapArr[1].NewColor := $FF008000;
ColorMapArr[2].OldColor := $FF0000FF;
ColorMapArr[2].NewColor := $FF000080;
Attr.SetRemapTable(ColorMapArr);
Rect.Offset(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
{ 枚舉原圖片中調色板的顏色 }
Rect.Offset(-Rect.X + 10, Rect.Height + 10);
Rect.Width := 15;
Rect.Height := 15;
Brush := TGPSolidBrush.Create(0);
ColorPalette := Img.Palette;
for i := 0 to ColorPalette.Count - 1 do
begin
Brush.Color := ColorPalette.EntrIEs[i];
Graphics.FillRectangle(Brush, Rect);
Rect.Offset(20, 0);
end;
{ 枚舉變換後的調色板的顏色 }
Rect.X := Img.Width + 20;
Attr.GetAdjustedPalette(ColorPalette, ColorAdjustTypeBitmap);
for i := 0 to ColorPalette.Count - 1 do
begin
Brush.Color := ColorPalette.EntrIEs[i];
Graphics.FillRectangle(Brush, Rect);
Rect.Offset(20, 0);
end;
end;
SetColorMatrices 測試:
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics: IGPGraphics;
Img: IGPImage;
Attr: IGPImageAttributes;
ColorMatrix: TGPColorMatrix;
Rect: TGPRectF;
begin
Graphics := TGPGraphics.Create(Handle);
Graphics.FillRectangle(TGPSolidBrush.Create($FFFF0000), TGPRect.Create(ClIEntRect));
Img := TGPImage.Create('C:\GdiPlusImg\Bird.bmp');
Rect.Initialize(4, 4, Img.Width, Img.Height);
Attr := TGPImageAttributes.Create;
{ 原始圖片 }
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);
ColorMatrix.SetToIdentity;
ColorMatrix.M[0, 0] := 1.5;
ColorMatrix.M[1, 1] := 1;
ColorMatrix.M[2, 2] := 0.5;
{ 變換包括 RGB 與灰度 }
Attr.SetColorMatrices(ColorMatrix, ColorMatrix, ColorMatrixFlagsDefault);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
{ 變換只有 RGB }
Attr.SetColorMatrices(ColorMatrix, ColorMatrix, ColorMatrixFlagsSkipGrays);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
{ 變換只有灰度 }
Attr.SetColorMatrices(ColorMatrix, ColorMatrix, ColorMatrixFlagsAltGray);
Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, Attr);
end;