How to display TBitmap with alpha channel on TImage?

I have a TBitmap that contains a translucent image with an alpha channel (in this example, I got it from TPngImage).

var SourceBitmap: TBitmap; PngImage: TPngImage; begin PngImage := TPngImage.Create(); SourceBitmap := TBitmap.Create(); try PngImage.LoadFromFile('ImgSmallTransparent.png'); SourceBitmap.Assign(PngImage); SourceBitmap.SaveToFile('TestIn.bmp'); imgSource.Picture.Assign(SourceBitmap); finally PngImage.Free(); SourceBitmap.Free(); end; 

When I save this TBitmap in the TestIn.bmp file and open it using any image viewer, I see transparency. But when I assign it to TImage, the transparent pixels appear black (TImage has Transparent = True ).

How to display TBitmap with transparency on TImage?

+3
delphi gdi timage
source share
1 answer

Your displayed code works fine on my system if I use Transparent = false for imgSource.
I can reproduce behavoiur with black pixels if I load a bitmap from a file.

Various settings affect the display. enter image description hereenter image description hereenter image description hereenter image description here

 procedure TForm3.SetAlphaFormatClick(Sender: TObject); begin if SetAlphaFormat.Checked then ToggleImage.Picture.Bitmap.alphaformat := afDefined else ToggleImage.Picture.Bitmap.alphaformat := afIgnored; end; procedure TForm3.SetImageTransparentClick(Sender: TObject); begin ToggleImage.Transparent := SetImageTransparent.Checked; Image1.Transparent := SetImageTransparent.Checked; end; procedure TForm3.LoadPngTransform2BitmapClick(Sender: TObject); Const C_ThePNG = 'C:\temp\test1.png'; C_TheBitMap = 'C:\temp\TestIn.bmp'; var SourceBitmap, TestBitmap: TBitmap; pngImage: TPngImage; begin Image1.Transparent := SetImageTransparent.Checked; pngImage := TPngImage.Create; SourceBitmap := TBitmap.Create; TestBitmap := TBitmap.Create; try pngImage.LoadFromFile(C_ThePNG); SourceBitmap.Assign(pngImage); {v1 with this version without the marked* part, I get the behavoir you described SourceBitmap.SaveToFile(C_TheBitMap); TestBitmap.LoadFromFile(C_TheBitMap); TestBitmap.PixelFormat := pf32Bit; TestBitmap.HandleType := bmDIB; TestBitmap.alphaformat := afDefined; //* Image1.Picture.Assign(TestBitmap); } //> v2 SourceBitmap.SaveToFile(C_TheBitMap); SourceBitmap.PixelFormat := pf32Bit; SourceBitmap.HandleType := bmDIB; SourceBitmap.alphaformat := afDefined; Image1.Picture.Assign(SourceBitmap); //< v2 finally pngImage.Free; SourceBitmap.Free; TestBitmap.Free; end; end; 
+5
source share

All Articles