Delphi   发布时间:2022-04-10  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了delphi – 如何使用纯GDI对画布区域进行颜色混合(按指定的alpha值着色)?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用纯 Windows GDI(不使用GDI,DirectX或类似,没有OpenGL,没有汇编程序或第三方库)对画布区域进行颜色混合(使用指定的alpha值着色).

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

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,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.bottom - ARect.Top,DC,BlendFunction);
  finally
    deleteObject(Brush);
    deleteObject(Bitmap);
    deleteDC(DC);
  end;
end;

有关此功能应该做什么的概念,请参阅以下内容(区分:-)图像:

并且代码可以通过上面显示的方式将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,ImagE);
  finally
    Image.Free;
  end;
end;

使用纯GDI或Delphi VCL有更有效的方法吗?

解决方法

你是否尝试过AlphaBlend的Canvas绘图?

就像是

Canvas.Draw(Arect.Left,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,bmp.Width,bmp.Height));
    ACanvas.Draw(ARect.Left,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);
    // then for fun do it to the Form itself
    ColorBlend(Canvas,ClientRect,clYellow,15);
  finally
    Image.Free;
  end;
end;

大佬总结

以上是大佬教程为你收集整理的delphi – 如何使用纯GDI对画布区域进行颜色混合(按指定的alpha值着色)?全部内容,希望文章能够帮你解决delphi – 如何使用纯GDI对画布区域进行颜色混合(按指定的alpha值着色)?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。