简体   繁体   English

调整 PNG 图像大小

[英]Resize PNG image

HI!你好! Could you please tell me how to resize a .png image.你能告诉我如何调整 .png 图像的大小吗? Or better give an example.或者最好举个例子。 I've been searching for the answer for a long time and it seems that nobody knows how to resize a .png image and keep its transparency.我一直在寻找答案,似乎没有人知道如何调整 .png 图像的大小并保持其透明度。 :( :(

The original author of the PNGImage component (the basis of the Delphi native component) had a forum where he, and others, posted code snippets on how to do things using the PNGImage component. PNGImage 组件(Delphi 原生组件的基础)的原作者有一个论坛,他和其他人在那里发布了关于如何使用 PNGImage 组件做事的代码片段。

Before the forum was taken down I grabbed a copy of all of the code snippets and placed them on the CodeGear Code Central website.在论坛关闭之前,我抓取了所有代码片段的副本并将它们放在 CodeGear 代码中心网站上。

Most if not all of these work with native PNG images and do maintain the Alpha channel.大多数(如果不是全部)都适用于本机 PNG 图像,并且确实保持 Alpha 通道。

Here is the complete list of examples included in the package:以下是软件包中包含的示例的完整列表:

  • Smooth rotates a PNG object平滑旋转 PNG 对象
  • Resizes a TPNGObject using a smooth algorithm使用平滑算法调整 TPNGObject 的大小
  • Slice one PNG into several smaller ones将一个 PNG 切成几个较小的
  • Saves an image as either a bitmap or a png.将图像保存为位图或 png。
  • Sample chunk descendant样本块后代
  • Read all tEXt-Chunks and write values into a TStrings object读取所有 tEXt-Chunks 并将值写入 TStrings 对象
  • Display a message box with information extracted from the PNG File显示一个消息框,其中包含从 PNG 文件中提取的信息
  • Finds and cuts a block from a PNG image从 PNG 图像中查找并剪切块
  • This method converts the png into a jpeg object此方法将 png 转换为 jpeg 对象
  • This method converts the png into a bmp object此方法将png转换为bmp对象
  • Overlay one PNG over another将一个 PNG 覆盖在另一个上
  • This makes the image half transparent这使图像半透明
  • Flips a png image vertically and saves back垂直翻转 png 图像并保存回来
  • Draws a png image over the desktop在桌面上绘制一个 png 图像

Here is the link: CodeCentral PNG Methods这是链接: CodeCentral PNG 方法

You can use Windows Imaging Component (WIC) in Delphi 2010. You can load your PNG image in a TWICImage class, then extract IWICBitmapScaler interface from its handle.您可以在 Delphi 2010 中使用Windows Imaging Component (WIC)。您可以在TWICImage类中加载您的 PNG 图像,然后从其句柄中提取 IWICBitmapScaler 接口。

Using IWICBitmapScaler , you can scale down or up the image.使用IWICBitmapScaler ,您可以缩小或放大图像。

WIC is available on Windows Vista, and Windows 7. For Windows XP, you have to install an update before using it. WIC 在 Windows Vista 和 Windows 7 上可用。对于 Windows XP,您必须在使用前安装更新。

There are versions of PngImage (Portable Network Graphics Delphi) That allow to do it via simple StretchDraw.有一些版本的 PngImage(便携式网络图形 Delphi)允许通过简单的 StretchDraw 来完成。

I have version which allows to do it - 1.564 (31 July 2006)我有允许这样做的版本 - 1.564(2006 年 7 月 31 日)

And version which not allows - 1.4361 (8 March 2003)和不允许的版本 - 1.4361(2003 年 3 月 8 日)

To perform it I used:为了执行它,我使用了:

heart.png - with transparency and I managed to resize it and save with transparency. heart.png - 具有透明度,我设法调整它的大小并以透明度保存。

empty.png - the pure transparent png. empty.png - 纯透明的 png。 it was used as blank sheet to put my image on it.它被用作空白纸来放置我的图像。

I have checked it via such code:我已经通过这样的代码检查过:

procedure TForm1.Button1Click(Sender: TObject);
var pic_empty, pic_stamp, pic_result :TPicture;
   r:TRect;
   png : TPNGObject;
begin

  pic_stamp := TPicture.Create;
  pic_stamp.LoadFromFile('c:\heart.png');
  pic_stamp.Graphic.Transparent := True;

  pic_empty := TPicture.Create;
  pic_empty.LoadFromFile('c:\empty.png');
  pic_empty.Graphic.Transparent := True;

  r.Left := 0;
  r.Top := 0;
  r.Right := r.Left + 100;
  r.Bottom := r.Top + 100;

  pic_result := tpicture.Create;
  pic_result.Bitmap.Assign(pic_empty.Graphic);
  pic_result.Graphic.Transparent := True;
  pic_result.Bitmap.Canvas.StretchDraw(r,pic_stamp.Graphic);
  pic_result.Bitmap.Width :=100;
  pic_result.Bitmap.Height:=100;

  png := TPNGObject.Create;
  png.Assign(pic_result.Bitmap);
  png.SaveToFile('c:\result.png');

  png.Free;
  pic_result.Free;
  pic_empty.Free;
  pic_stamp.free;

end;

Delphi-7, win7 x64 Delphi-7, win7 x64

Unfortunately, the vcldeveloper did not give an example in his answer .不幸的是, vcldeveloper在他的回答中没有举例说明。

Here is a small example.这是一个小例子。

var
  vImage: TWICImage;
  vScaler: IWICBitmapScaler;
begin
   ...

    vImage := TWICImage.Create;
    try
      ...

      vImage.ImagingFactory.CreateBitmapScaler(vScaler);
      vScaler.Initialize(vImage.Handle, NewWidth, NewHeight, WICBitmapInterpolationModeFant);

      vImage.Handle := IWICBitmap(vScaler);

      ...
    finally
      vImage.Free;
    end 

More examples can be found in the Delphi source.更多示例可以在 Delphi 源中找到。

I must confess that I don't have any experience in programmatically play with png.我必须承认,我在以编程方式玩 png 方面没有任何经验。
Anyway, you will find some libs here .不管怎样,你会在这里找到一些库。 Except the native Delphi PNG support, I think you'll find there all the existing libs (native to delphi of course).除了原生的 Delphi PNG 支持,我想你会在那里找到所有现有的库(当然是原生的 delphi)。

If nothing helps you there, consider playing with ImageMagick if possible...That's the Swiss army knife of the image manipulation and all It can do is feasible in command line如果没有任何帮助,请考虑使用 ImageMagick 如果可能的话......这是图像处理的瑞士军刀,它可以做的所有在命令行中都是可行的

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

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