gpt4 book ai didi

delphi - 是否可以使用 TImage 和 GDIPlus 打开图像并保持完全透明度

转载 作者:行者123 更新时间:2023-12-03 15:57:11 25 4
gpt4 key购买 nike

我可以使用 GDIPlus 在 TImage 中显示一个 32 位图像,该图像具有部分( mask )透明度,但 alpha 值为 0 或 255,中间没有任何值。我尝试加载 PngImage、32 位位图和图标都产生相同的...蒙版透明度,但不是完全透明。

是否有另一种方法可以让 TImage 显示完全透明的 GDI+ 图形,如所需结果图像所示?

enter image description here打开后的GDI Plus

enter image description here期望的结果

procedure TFormMain.Open1Click ( Sender: TObject );
// Load a GDIPlus Bitmap into TImage
var
GPBitmap: TGPBitmap;
iHBitmap: HBITMAP;
iStatus: TStatus;
const
TRANS_COLOR = clBlack;
begin

if OpenPictureDialog1.Execute then
begin

FilePath := OpenPictureDialog1.FileName;
begin

GPBitmap := TGpBitmap.Create ( FilePath );
try

iStatus := GPBitmap.GetHBITMAP ( aclBlack, iHBitmap );
// As best as I can determine from the internet, the GetHBitmap which is needed to assign a GPbitmap to TImage
// does not hold an alphachannel, so loaded images alpha are either 0 or 255, but drawing with alphachannel values does work.
if iStatus = Ok then
begin

Image1.Picture.Bitmap.Handle := iHBitmap;
Image1.Picture.Bitmap.TransparentColor := Image1.Picture.Bitmap.Canvas.Pixels [ 0, Image1.Picture.Bitmap.Height - 1 ];
StatusBar1.Panels [ 0 ].Text := FileCtrl.MinimizeName ( ExtractFileDir ( FilePath ), Canvas, 200 ); // Folder
StatusBar1.Panels [ 1 ].Text := FileCtrl.MinimizeName ( ExtractFileName ( FilePath ), Canvas, 75 ); // Filename
StatusBar1.Panels [ 2 ].Text := 'Width: ' + IntegerToString ( Image1.Picture.Bitmap.Width ); // Width
StatusBar1.Panels [ 3 ].Text := 'Height: ' + IntegerToString ( Image1.Picture.Bitmap.Height ); // Height
StatusBar1.Panels [ 4 ].Text := BitDepthToColorString ( GetPixelFormatSize ( GPBitmap.GetPixelFormat ) ); // Bitdepth

Image1.Refresh;

end;

finally
GPBitmap.Free;
end;

end;

end;

end;

最佳答案

您需要做的是选择一种背景颜色,该颜色在传输图像时会与绘制图像的表面混合。 IE。而不是

iStatus := GPBitmap.GetHBITMAP ( aclBlack, iHBitmap );

使用

iStatus := GPBitmap.GetHBITMAP(ColorRefToARGB(ColorToRGB(clBtnFace)), iHBitmap);

例如,如果图像采用默认彩色形式。

此时您已经知道图像没有 Alpha channel ,但它已使用合适的颜色进行了展平。 “TBitmap”的 Transparent.. 属性对部分透明没有帮助。如果您设置 TBitmapTransparent 属性,当您将其放在不同颜色的背景上时,它仍然会像问题的第一张图像一样,可能具有更好的边缘颜色。无论如何,如果您使用它,请不要忘记将“TransparentColor”设置为“clBtnFace”而不是“clBlack”。

关于delphi - 是否可以使用 TImage 和 GDIPlus 打开图像并保持完全透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10143422/

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