我创建了一个基于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;
答案 0 :(得分:0)
创建位图时,默认情况下,所有像素均设置为特定值。在我的测试中,该值为$ FFFFFFFF。调用Pixel [0,0]时,它将读取该位置的颜色,并将其转换为RGB。在这种情况下,$ FFFFFF。这就是您的TransparentColor。渲染位图时,它将每个像素值与TransparentColor进行比较。匹配的是透明的。但是$ FFFFFFFF与$ FFFFFF不匹配,因此这些像素不是透明的。
要使像素透明,请明确将其设置为所需的颜色。