gpt4 book ai didi

delphi - 如何在 TImage 上正确显示带有 alpha channel 的 TBitmap?

转载 作者:行者123 更新时间:2023-12-03 15:50:22 27 4
gpt4 key购买 nike

我有一个 TBitmap,其中包含带有 alpha channel 的半透明图像(在本例中我从 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;

当我将此 TBitmap 保存到 TestIn.bmp 文件并使用任何图像查看器打开它时,我可以看到透明度。但是当我将它分配给 TImage 时,透明像素显示为黑色(TImage 有 Transparent = True)。

如何在TImage上正确显示透明的TBitmap?

最佳答案

如果我对 imgSource 使用Transparent=false,您显示的代码在我的系统上可以正常工作。
如果我从文件加载位图,我可以用黑色像素重现行为。

不同的设置会影响显示 enter image description here enter image description here enter image description here enter 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;

关于delphi - 如何在 TImage 上正确显示带有 alpha channel 的 TBitmap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17614566/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com