delphi - 如何使用纯 GDI 对 Canvas 区域进行颜色混合(按指定的 alpha 值着色)?

标签 delphi winapi optimization gdi

我想使用纯 Windows GDI 对 Canvas 区域进行颜色混合(按指定的 alpha 值着色) (因此没有 GDI+、DirectX 或类似的、没有 OpenGL、没有汇编器或第 3 方库)。

我创建了以下函数,我想知道是否有更有效或更简单的方法来执行此操作:

procedure ColorBlend(const ACanvas: HDC; const ARect: TRect;
  const ABlendColor: TColor; const ABlendValue: Integer);
var
  DC: HDC;
  Brush: HBRUSH;
  Bitmap: HBITMAP;
  BlendFunction: TBlendFunction;
begin
  DC := CreateCompatibleDC(ACanvas);
  Bitmap := CreateCompatibleBitmap(ACanvas, ARect.Right - ARect.Left,
    ARect.Bottom - ARect.Top);
  Brush := CreateSolidBrush(ColorToRGB(ABlendColor));
  try
    SelectObject(DC, Bitmap);
    Windows.FillRect(DC, Rect(0, 0, ARect.Right - ARect.Left,
      ARect.Bottom - ARect.Top), Brush);
    BlendFunction.BlendOp := AC_SRC_OVER;
    BlendFunction.BlendFlags := 0;
    BlendFunction.AlphaFormat := 0;
    BlendFunction.SourceConstantAlpha := ABlendValue;
    Windows.AlphaBlend(ACanvas, ARect.Left, ARect.Top,
      ARect.Right - ARect.Left, ARect.Bottom - ARect.Top, DC, 0, 0,
      ARect.Right - ARect.Left, ARect.Bottom - ARect.Top, BlendFunction);
  finally
    DeleteObject(Brush);
    DeleteObject(Bitmap);
    DeleteDC(DC);
  end;
end;

有关此函数应执行的操作的概念,请参阅以下(有区别的:-)图像:

enter image description here enter image description here

以及可以渲染this image的代码按照上面所示的方式到表单的左上角:

uses
  PNGImage;

procedure TForm1.Button1Click(Sender: TObject);
var
  Image: TPNGImage;
begin
  Image := TPNGImage.Create;
  try
    Image.LoadFromFile('d:\6G3Eg.png');
    ColorBlend(Image.Canvas.Handle, Image.Canvas.ClipRect, $0000FF80, 175);
    Canvas.Draw(0, 0, Image);
  finally
    Image.Free;
  end;
end;

有没有更有效的方法使用纯 GDI 或 Delphi VCL 来做到这一点?

最佳答案

您尝试过使用 AlphaBlend 进行 Canvas 绘图吗?

类似的东西

Canvas.Draw(Arect.Left, ARect.Top, ABitmap, AAlphaBlendValue);

与 FillRect 结合用于混合颜色

更新:这里有一些代码,尽可能接近您的界面,但纯 VCL。
可能没有那么高效,但更简单(并且有些便携)。
正如 Remy 所说,要以伪持久方式在 Form 上绘画,您必须使用 OnPaint...

procedure ColorBlend(const ACanvas: TCanvas; const ARect: TRect;
  const ABlendColor: TColor; const ABlendValue: Integer);
var
  bmp: TBitmap;
begin
  bmp := TBitmap.Create;
  try
    bmp.Canvas.Brush.Color := ABlendColor;
    bmp.Width := ARect.Right - ARect.Left;
    bmp.Height := ARect.Bottom - ARect.Top;
    bmp.Canvas.FillRect(Rect(0,0,bmp.Width, bmp.Height));
    ACanvas.Draw(ARect.Left, ARect.Top, bmp, ABlendValue);
  finally
    bmp.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Image: TPNGImage;
begin
  Image := TPNGImage.Create;
  try
    Image.LoadFromFile('d:\6G3Eg.png');
    ColorBlend(Image.Canvas, Image.Canvas.ClipRect, $0000FF80, 175);
    Canvas.Draw(0, 0, Image);
    // then for fun do it to the Form itself
    ColorBlend(Canvas, ClientRect, clYellow, 15);
  finally
    Image.Free;
  end;
end;

关于delphi - 如何使用纯 GDI 对 Canvas 区域进行颜色混合(按指定的 alpha 值着色)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10541811/

相关文章:

sql-server-2005 - 在 Delphi XE 中读取 SQL Server 字段文本值并同时进行字符转换

delphi - TTabControl : tabs displayed right to left

delphi - 如何制作加载到 TImage.Picture 中的 WMF 文件的位图版本并将其移动到 TSpeedButton.Glyph

c# - 比反复试验更有效地优化多个变量的算法

php - MySQL进程和连接

php - 微优化值得花时间吗?

delphi - 相同的代码,缺少 qtintf70.dll

winapi - 透明顶层窗口

winapi - 在线程之间共享对象

c++ - Windows 消息有多健壮?