【问题标题】:Transparent Bitmap on TGraphicControl componentTGraphicControl 组件上的透明位图
【发布时间】:2019-01-31 16:12:34
【问题描述】:

我创建了一个基于 TGraphicControl 的组件。 当我直接在画布上画线时,它是透明的。但是当我尝试在其画布上绘制位图时,它不会显示为透明。 如何在 TGraphicControls 画布上绘制透明位图? 谢谢

unit Test;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,Types,DB;

type
  TTest = class(TgraphicControl)
  private
    { Private declarations }
    BMP:TBitmap;
  protected
    { Protected declarations }
    Procedure Paint; Override;
  public
    { Public declarations }
    Constructor create(Aoner: Tcomponent); Override;
    Destructor Destroy; Override;
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Automation', [TTest]);
end;

Constructor TTest.create(Aoner: TComponent);
begin
  Inherited Create(Aoner);
  Width:=100;
  Height:=100;
  Bmp := TBitmap.Create;
  BMp.Width:=50;
  BMP.Height:=50;
  BMP.Transparent:=true;
  BMP.TransparentColor:=BMp.Canvas.Pixels[0,0];
  BMP.Canvas.MoveTo(1,1);
  BMP.Canvas.LineTo(50,50);
End;

Procedure TTest.Paint;
Begin
  Inherited Paint;
  Canvas.MoveTo(0,0);
  Canvas.LineTo(100,100);
  Canvas.Draw(0,0,BMp);
End;

Destructor TTest.Destroy;
begin
  Inherited Destroy;
end;

【问题讨论】:

  • 只需在创建位图后添加Bmp.Canvas.Handle;,然后再设置其大小。如果您希望它通过看似有意义的方式来解决,请按照答案而不是Bmp.Canvas.Handle; 输入Bmp.Canvas.Brush.Color := clWhite; 之类的内容。
  • 您应该将此作为答案。调用 BMP.Canvas.Handle 确实可以解决问题。将其作为答案,并简要说明其工作原理。
  • 感谢您的评论。
  • @David - 抱歉,我不能。我认为重要的是在设置大小之前创建画笔,但即使在跟踪 graphics.pas 之后我也没有任何解释为什么......

标签: delphi custom-component


【解决方案1】:

创建位图时,默认情况下,所有像素都设置为特定值。在我的测试中,该值为 $FFFFFFFF。当您调用 Pixel[0,0] 时,它会读取该位置的颜色,并将其转换为 RGB。在这种情况下,$FFFFFF。这就是你的透明颜色。渲染位图时,它将每个像素值与透明颜色进行比较。那些匹配的是透明的。但是 $FFFFFFFF 与 $​​FFFFFF 不匹配,因此这些像素不透明。

要使像素透明,请将它们显式设置为您想要的颜色。

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    相关资源
    最近更新 更多