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

如何在Delphi中制作alpha透明TImage?

发布时间:2020-12-15 09:36:56 所属栏目:大数据 来源:网络整理
导读:在表格上我有两个T Images.位于顶部的T Image应该是透明的,因此我们可以看到底层的内容.如何改变TImage透明度的水平? 例: 解决方法 通常的方法是将所有图形绘制到一个目标画布(可能是TImage的位图),但即使有很多重叠的TImages,也可以这样做.请注意,您不能
在表格上我有两个T Images.位于顶部的T Image应该是透明的,因此我们可以看到底层的内容.如何改变TImage透明度的水平?

例:

解决方法

通常的方法是将所有图形绘制到一个目标画布(可能是TImage的位图),但即使有很多重叠的TImages,也可以这样做.请注意,您不能重叠TWinControls.
由于32位Bitmap支持透明度,因此可以通过将包含的图形转换为位图(如果需要)来实现.
通过设置Alphaformat:= afDefined,将使用来自alphachannel的透明度信息绘制位图.
我们需要一个位图的副本,因为设置AlphaFormat会让我们松开像素信息.
使用扫描线,可以将来自副本的像素信息传输到目的地,将alpha通道设置为所需的值.

“即发即忘”的实现可能如下所示:

type
  pRGBQuadArray = ^TRGBQuadArray;
  TRGBQuadArray = ARRAY [0 .. 0] OF TRGBQuad;

procedure SetImageAlpha(Image:TImage; Alpha: Byte);
var
  pscanLine32,pscanLine32_src: pRGBQuadArray;
  nScanLineCount,nPixelCount : Integer;
  BMP1,BMP2:TBitMap;
  WasBitMap:Boolean;
begin
  if assigned(Image.Picture) then
    begin
       // check if another graphictype than an bitmap is assigned
       // don't check Assigned(Image.Picture.Bitmap) which will return always true
       // since a bitmap is created if needed and the graphic will be discared   
       WasBitMap := Not Assigned(Image.Picture.Graphic);
       if not WasBitMap then
          begin   // let's create a new bitmap from the graphic
            BMP1 := TBitMap.Create;
            BMP1.Assign(Image.Picture.Graphic);
          end
       else BMP1 := Image.Picture.Bitmap;  // take the bitmap

       BMP1.PixelFormat := pf32Bit;

       // we need a copy since setting Alphaformat:= afDefined will clear the Bitmap
       BMP2 := TBitMap.Create;
       BMP2.Assign(BMP1);

       BMP1.Alphaformat := afDefined;

    end;
    for nScanLineCount := 0 to BMP1.Height - 1 do
    begin
      pscanLine32 := BMP1.Scanline[nScanLineCount];
      pscanLine32_src := BMP2.ScanLine[nScanLineCount];
      for nPixelCount := 0 to BMP1.Width - 1 do
        begin
          pscanLine32[nPixelCount].rgbReserved := Alpha;
          pscanLine32[nPixelCount].rgbBlue := pscanLine32_src[nPixelCount].rgbBlue;
          pscanLine32[nPixelCount].rgbRed  := pscanLine32_src[nPixelCount].rgbRed;
          pscanLine32[nPixelCount].rgbGreen:= pscanLine32_src[nPixelCount].rgbGreen;
        end;
    end;
    If not WasBitMap then
      begin  // assign and free Bitmap if we had to create it
      Image.Picture.Assign(BMP1);
      BMP1.Free;
      end;
    BMP2.Free; // free the copy
end;



procedure TForm3.Button1Click(Sender: TObject);
begin  // call for the example image
  SetImageAlpha(Image1,200);
  SetImageAlpha(Image2,128);
  SetImageAlpha(Image3,80);

end;

(编辑:李大同)

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

    推荐文章
      热点阅读