加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何使用纯GDI对画布区域进行颜色混合(按指定的alpha

发布时间:2020-12-15 04:03:11 所属栏目:大数据 来源:网络整理
导读:我想使用纯 Windows GDI (不使用GDI,DirectX或类似,没有OpenGL,没有汇编程序或第三方库)对画布区域进行颜色混合(使用指定的alpha值着色). 我已经创建了以下函数,我想知道是否有更高效或更简单的方法来执行此操作: procedure ColorBlend(const ACanvas: HDC;
我想使用纯 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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读