• ImageAttributes 的一些方法


    转载:https://my.oschina.net/hermer/blog/320889

    SetWrapMode() { 设置环绕模式 } 这是 IGPImageAttributes 中出了 Clone 以外唯一个和颜色不相关方法.

    WrapModeTile 测试:

     1 uses GdiPlus;
     2 
     3 procedure TForm1.FormPaint(Sender: TObject);
     4 var
     5   Graphics: IGPGraphics;
     6   Img: IGPImage;
     7   Attr: IGPImageAttributes;
     8   Rect: TGPRect;
     9 begin
    10   Graphics := TGPGraphics.Create(Handle);
    11   Img := TGPImage.Create('C:GdiPlusImgBird.bmp');
    12   Rect.Initialize(4, 4, Img.Width, Img.Height);
    13   Attr := TGPImageAttributes.Create;
    14 
    15   { 原始 }
    16   Graphics.DrawImage(Img, Rect, 0, 0, Img.Width, Img.Height, UnitPixel, nil);
    17 
    18   { WrapModeTile }
    19   Attr.SetWrapMode(WrapModeTile);
    20   Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
    21   Graphics.DrawImage(Img, Rect, 0, 0, Img.Width*2, Img.Height*2, UnitPixel, Attr);
    22 
    23   { WrapModeTileFlipX }
    24   Attr.SetWrapMode(WrapModeTileFlipX);
    25   Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
    26   Graphics.DrawImage(Img, Rect, 0, 0, Img.Width*2, Img.Height*2, UnitPixel, Attr);
    27 
    28   { WrapModeTileFlipY }
    29   Attr.SetWrapMode(WrapModeTileFlipY);
    30   Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
    31   Graphics.DrawImage(Img, Rect, 0, 0, Img.Width*2, Img.Height*2, UnitPixel, Attr);
    32 
    33   { WrapModeTileFlipXY }
    34   Attr.SetWrapMode(WrapModeTileFlipXY);
    35   Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
    36   Graphics.DrawImage(Img, Rect, 0, 0, Img.Width*2, Img.Height*2, UnitPixel, Attr);
    37 
    38   { WrapModeClamp }
    39   Attr.SetWrapMode(WrapModeClamp);
    40   Graphics.TranslateTransform(Rect.Width + Rect.X, 0);
    41   Graphics.DrawImage(Img, Rect, 0, 0, Img.Width*2, Img.Height*2, UnitPixel, Attr);
    42 end;

    SetThreshold()、SetThreshold() { 设置、取消 "阈值" } 取值范围: 0..1 假如设置阀值为 0.5, 那么超过 128 的红色都变为 256, 少于 128 的红色都变为 0; 绿色、蓝色也是如此.

    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()、ClearRemapTable()、SetBrushRemapTable()、ClearBrushRemapTable { 设置、取消 "颜色映射表" } 就是用另一种颜色替换指定颜色; 其主要参数是个数组, 可以替换多种颜色; SetBrushRemapTable ClearBrushRemapTable 专用于图元文件的画刷; SetRemapTable 也可以完成 SetBrushRemapTable 的任务, 所以感觉 SetBrushRemapTable 有点多余.

    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:GdiPlusImgSampleMetafile.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()、ClearColorKey() { 设置、取消透明色范围 } 如果只指定一种透明色, 可以把两个参数值设为相同.

    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:GdiPlusImgBird.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()、ClearGamma() { 设置、取消伽玛校正值(或叫灰度校正值) } 可以用它调整亮度; 取值范围: 0.1..5, 默认 1

    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:GdiPlusImgGrapes.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()、ClearOutputChannel() { 设置、取消 CMYK 输出通道 } 看到的 CMYK(Cyan、Magenta、Yellow、Black)各通道的效果都是灰度, 其实是颜色的强度.

    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:GdiPlusImgBird.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()、Reset() { 重置(回复默认值) } 重置时, 实用感觉是重置指定类型最好用 SetToIdentity, 全部重置用 Reset. SetNoOp()、ClearNoOp() { 禁用、取消 SetNoOp 的禁用(也就是再使用) }

    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:GdiPlusImgSampleMetafile.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:GdiPlusImgGrapes.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() { 获取变换后的调色板 } 即使是在颜色变换后, IGPImage 获取的调色板也还是原来的; 获取变更后的调色板需要 IGPImageAttributes.GetAdjustedPalette().

    SetColorMatrix()、ClearColorMatrix() { 设置、取消颜色调整矩阵 }

    SetColorMatrices()、ClearColorMatrices() { 设置、取消颜色调整矩阵与灰度调整矩阵}

    SetColorMatrices 的参数中有两个矩阵, 前者用于颜色矩阵、后者用于灰度矩阵. SetColorMatrices 的第三个参数需要个

    TGPColorMatrixFlags 类型的枚举值:

    TGPColorMatrixFlags = ( ColorMatrixFlagsDefault : //只使用第一个矩阵调整颜色与灰度, 这和使用 SetColorMatrix 效果相同; ColorMatrixFlagsSkipGrays: //只调整颜色矩阵, 同样会忽略第二个矩阵; ColorMatrixFlagsAltGray : //只调整灰色矩阵, 此时一个矩阵被忽略. );

    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:GdiPlusImgStripes.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:GdiPlusImgBird.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;

    SetOutputChannelColorProfile()、ClearOutputChannelColorProfile() { 设置、取消颜色配置文件} *.icc *.icm; 默认路径是 ...WINDOWSsystem32spooldriverscolor

    上述方法除 SetWrapMode、SetBrushRemapTable、ClearBrushRemapTable 外,
    都有个 TGPColorAdjustType 类型的参数:

    TGPColorAdjustType = ( ColorAdjustTypeDefault, // 默认, 适应用各类型 ColorAdjustTypeBitmap, // 用于位图 ColorAdjustTypeBrush, // 用于图元文件中的画刷 ColorAdjustTypePen, // 用于图元文件中的画笔 ColorAdjustTypeText, // 用于图元文件中的文本的画刷 ColorAdjustTypeCount, // 无用 ColorAdjustTypeAny // 保留 ); //如果是 ColorAdjustTypeDefault, 那么变换将应用到各类型(位图、画笔、画刷等); //使用 Default 后, 如果再单独指定变换, 当然它不会再使用 Default 值, 但 Clear 后就回不到 Default 了.

  • 相关阅读:
    Citrix Receiver running on my mobile phone
    is undfined javascript error
    系统架构设计随笔
    计算机与数理化“最高”期刊之比较zt
    Tikhonov regularization
    关于Likelihood 和 Probability的差别
    Cross Validation
    八卦 Knuth zt
    Eclipse切换IDE界面语言
    数学家对数学的论述
  • 原文地址:https://www.cnblogs.com/Toya/p/13746376.html
Copyright © 2020-2023  润新知