例:
解决方法 通常的方法是将所有图形绘制到一个目标画布(可能是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 copyend;procedure TForm3.button1Click(Sender: TObject);begin // call for the example image SetimageAlpha(Image1,200); SetimageAlpha(Image2,128); SetimageAlpha(Image3,80);end;总结
以上是内存溢出为你收集整理的如何在Delphi中制作alpha透明TImage?全部内容,希望文章能够帮你解决如何在Delphi中制作alpha透明TImage?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)