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;