And*_*rew 3 delphi gdi timage tbitmap
我有一个TBitmap,其中包含带alpha通道的半透明图像(在本例中我是从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;
Run Code Online (Sandbox Code Playgroud)
当我将此TBitmap保存到TestIn.bmp文件并使用任何图像查看器打开它时,我可以看到透明度.但是当我将它分配给TImage时,透明像素显示为黑色(TImage有Transparent = True).
如何在TImage上正确显示具有透明度的TBitmap?
如果我对imgSource使用Transparent = false,那么您显示的代码在我的系统上运行正常.
如果我从文件加载位图,我可以使用黑色像素重现行为.
不同的设置会影响显示

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;
Run Code Online (Sandbox Code Playgroud)