繁体   English   中英

如何使用GDI +使用Alpha通道调整PNG图像的大小?

[英]How to resize PNG image with Alpha channels using GDI+?

寻找一种调整TPngObject大小并保持透明度+ Alpha通道无济于事的方法后,我尝试使用GDI +

这是我的代码,它似乎工作正常。 它将缩小/放大PNG。 到目前为止,已经在XP上进行了测试:

uses GDIPAPI, GDIPOBJ, GDIPUTIL; 

procedure TForm1.Button1Click(Sender: TObject);
var
  encoderClsid: TGUID;
  stat: TStatus;
  img, img_out: TGPImage;
begin
  img := TGPImage.Create('in.png'); // 200 x 200  
  img_out := img.GetThumbnailImage(100, 100, nil, nil);
  GetEncoderClsid('image/png', encoderClsid);
  img_out.Save('out.png', encoderClsid);
  img_out.free;
  img.Free;
end;

我的问题:使用GetThumbnailImage是正确的方法吗? 我没有找到其他方法。

我认为GetThumbnailImage方法不是一个好方法,因为我怀疑您将获得高质量的重采样图像。 本文中,您可以找到如何重新缩放图像。 他们为此使用了DrawImage方法,所以我会做同样的事情。 在此之前,我还将设置高质量的图形模式以获得高质量的输出。 这是一个例子:

procedure TForm1.Button1Click(Sender: TObject);
var
  Input: TGPImage;
  Output: TGPBitmap;
  Encoder: TGUID;
  Graphics: TGPGraphics;
begin
  Input := TGPImage.Create('C:\InputImage.png');
  try
    // create the output bitmap in desired size
    Output := TGPBitmap.Create(100, 100, PixelFormat32bppARGB);
    try
      // create graphics object for output image
      Graphics := TGPGraphics.Create(Output);
      try
        // set the composition mode to copy
        Graphics.SetCompositingMode(CompositingModeSourceCopy);
        // set high quality rendering modes
        Graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
        Graphics.SetPixelOffsetMode(PixelOffsetModeHighQuality);
        Graphics.SetSmoothingMode(SmoothingModeHighQuality);
        // draw the input image on the output in modified size
        Graphics.DrawImage(Input, 0, 0, Output.GetWidth, Output.GetHeight);
      finally
        Graphics.Free;
      end;
      // get encoder and encode the output image
      if GetEncoderClsid('image/png', Encoder) <> -1 then
        Output.Save('C:\OutputImage.png', Encoder)
      else
        raise Exception.Create('Failed to get encoder.');
    finally
      Output.Free;
    end;
  finally
    Input.Free;
  end;
end;

我不认为使用GetThumbnailImage方法是正确的方法。 为什么?

GetThumbnailImage方法的主要用途是获取缩略图,您可以将其用作更高分辨率图像的预览。

因此,我假设后面使用的算法被开发为尽可能快,但是它可能不太在乎最终结果的质量。 因此,使用此方法可能会导致调整后的图像质量很差。


现在,如果您真的对使用Delphi进行图像处理感兴趣,那么您绝对应该检查Graphics32库( http://graphics32.org/wiki/ )。

它支持Delphi 7及更高版本中的所有Delphi版本。 它提供了许多高级图像处理算法。 最棒的是,它确实支持硬件加速,这意味着它实际上可以利用GPU的处理能力来进行这些图像处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM