【问题标题】:Transparent PNG image loaded from resource file, resized with Grapics32 and drawn on the Canvas从资源文件加载的透明 PNG 图像,使用 Grapics32 调整大小并在 Canvas 上绘制
【发布时间】:2020-05-11 06:31:29
【问题描述】:

我需要一点帮助...

我的应用程序资源中有一个透明的 PNG 图像。到目前为止,我将它加载到 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

我在这里找到了similar question,但我没有使用带有TImage 的图像,我直接在画布上绘制它。在单一的答案中,大卫说:

无论如何,如果是这样,我会结合透明度支持 具有TBitmap32重采样能力的TImage构建解决方案 那样。将原始图像保存在 TBitmap32 实例中。每当 您需要将其加载到 TImage 组件中,例如当 重新调整大小,使用 TBitmap32 执行内存调整大小并加载 调整大小的图像。

这正是我想要做的,但我不知道为什么透明度不起作用。有任何想法吗 ?

【问题讨论】:

  • 你能附上一张预期和当前问题的图片吗?我的意思是,如果画布上图像的背景很简单(纯单色),那么为什么不先填充 FOTO32 背景,然后在其上绘制 FOTOPNG,然后再绘制到画布上呢?。
  • 画布的背景不是实心的,有其他的图画。
  • FotoPng.Draw(Foto32.Canvas, ... -> 一旦你画了 png,它就结束了。您可以透明地绘制,但透明度信息在那里丢失,绘制不会将 Alpha 通道传输到目标。您需要重新采样 png 本身。见here
  • @SertacAkyuz 我使用了该代码,但出现异常“仅支持 COLOR_RGBALPHA 和 COLOR_RGB 格式”...因为我的 png 图像太大,我不得不用 `pngquant' 对其进行压缩。 :(
  • 能否将pngquant解压缩为内存中的普通png?

标签: delphi resize transparency delphi-10.3-rio graphics32


【解决方案1】:

您的问题似乎是将缓冲区绘制到屏幕上的问题。 Bitmap32 使用StretchDIBits 进行忽略alpha 通道的绘画。

您可以使用AlphaBlend 函数来绘制图像:

procedure TForm1.FormPaint(Sender: TObject);
var
  BF: TBlendFunction;
begin
  BF.BlendOp := AC_SRC_OVER;
  BF.BlendFlags := 0;
  BF.SourceConstantAlpha := 255;
  BF.AlphaFormat := AC_SRC_ALPHA;

  Winapi.Windows.AlphaBlend(Canvas.Handle, 0, 0, Buff.Width, Buff.Height,
    Buff.Canvas.Handle, 0, 0, Buff.Width, Buff.Height, BF);
end;

或者将您的 TBitmap32 转换为 Delphi TBitmap 并使用 VCL 进行绘制:

procedure TForm1.FormPaint(Sender: TObject);
var
  Bmp: TBitmap;
  I: Integer;
begin
  Bmp := TBitmap.Create;
  try
    Bmp.PixelFormat := pf32bit;
    Bmp.AlphaFormat := afDefined;
    Bmp.SetSize(Buff.Width, Buff.Height);
    for I := 0 to Buff.Height - 1 do
      Move(Buff.ScanLine[I]^, Bmp.ScanLine[I]^, Buff.Width * 4);
    Canvas.Draw(0, 0, Bmp);
  finally
    Bmp.Free;
  end;
end;

【讨论】:

  • Bmp.AlphaFormat := afDefined; 是关键! :) 坦克!
猜你喜欢
  • 2014-04-14
  • 1970-01-01
  • 2019-03-16
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
相关资源
最近更新 更多