代码之家  ›  专栏  ›  技术社区  ›  Fayyaz

控制组件上的透明位图

  •  1
  • Fayyaz  · 技术社区  · 6 年前

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

    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;
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   David Dubois    6 年前

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

    要使像素透明,请显式将其设置为所需的颜色。