我需要一点帮助...
我的应用程序资源中有一个透明的 PNG 图像。到目前为止,我将它加载到 a 中TPngImage
并使用Canvas.Draw(X, Y, PngImage);
. 它是透明绘制的。现在我将我的应用程序更新为 DpiAware,我需要缩放所有图像。我需要一个高质量的重采样器,我选择使用 Graphics32。我设法进行了重新采样,但我不知道如何保持透明度......我尝试了所有我能想到的......以下代码的结果是在透明区域中用黑色绘制的图像...... .
Foto32, Buff: TBitmap32;
FotoPng: TPngImage;
constructor TForm.Create(AOwner: TComponent);
const BkgHeight = 380;
var Res: TKernelResampler;
SRect, DRect: TRect;
ImgWidth: Integer;
begin
inherited;
Buff:= TBitmap32.Create;
Res:= TKernelResampler.Create;
Res.Kernel:= TLanczosKernel.Create;
FotoPng:= TPngImage.Create;
FotoPng.Transparent:= True;
FotoPng.TransparentColor:= clBlack;
FotoPng.LoadFromResourceName(HInstance, 'BKG_FOTO');
Foto32:= TBitmap32.Create;
Foto32.DrawMode:= dmBlend;
Foto32.CombineMode:= cmMerge;
Foto32.OuterColor:= clBlack;
Foto32.Canvas.Brush.Style:= bsClear;
Foto32.SetSize(FotoPng.Width, FotoPng.Height);
FotoPng.Draw(Foto32.Canvas, Rect(0, 0, FotoPng.Width, FotoPng.Height));
ImgWidth:= Round(Real(Foto32.Width / Foto32.Height) * BkgHeight);
SRect:= Rect(0, 0, Foto32.Width, Foto32.Height);
Buff.DrawMode:= dmBlend;
Buff.CombineMode:= cmMerge;
Buff.OuterColor:= clBlack;
Buff.Canvas.Brush.Style:= bsClear;
Buff.SetSize(Scale(ImgWidth), Scale(BkgHeight));
DRect:= Rect(0, 0, Buff.Width, Buff.Height);
Res.Resample(Buff, DRect, DRect, Foto32, SRect, dmTransparent {dmBlend}, nil);
end;
procedure TForm.Paint;
begin
// ....
Buff.DrawTo(Canvas.Handle, X, Y);
end;
这是我编译成资源的透明PNG图像: https ://postimg.cc/3yy3wrJB
我在这里找到了一个类似的问题,但我不使用带有 a 的图像TImage
,我直接在画布上绘制它。在唯一的答案中,大卫说:
无论如何,如果是这样,我会将 TImage 的透明度支持与 TBitmap32 的重新采样能力结合起来,以这种方式构建解决方案。将原始图像保存在 TBitmap32 实例中。每当您需要将其加载到 TImage 组件中时,例如在重新调整大小时,请使用 TBitmap32 执行内存中重新调整大小并加载该重新调整大小的图像。
这正是我想要做的,但我不知道为什么透明度不起作用。有任何想法吗 ?