Resize PNG image
Asked Answered
S

5

6

HI! Could you please tell me how to resize a .png image. 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. :(

Steinbach answered 13/3, 2010 at 8:8 Comment(0)
W
22

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.

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.

Most if not all of these work with native PNG images and do maintain the Alpha channel.

Here is the complete list of examples included in the package:

  • Smooth rotates a PNG object
  • Resizes a TPNGObject using a smooth algorithm
  • Slice one PNG into several smaller ones
  • Saves an image as either a bitmap or a png.
  • Sample chunk descendant
  • Read all tEXt-Chunks and write values into a TStrings object
  • Display a message box with information extracted from the PNG File
  • Finds and cuts a block from a PNG image
  • This method converts the png into a jpeg object
  • This method converts the png into a bmp object
  • Overlay one PNG over another
  • This makes the image half transparent
  • Flips a png image vertically and saves back
  • Draws a png image over the desktop

Here is the link: CodeCentral PNG Methods

Worth answered 13/3, 2010 at 18:27 Comment(1)
Just replace TPNGObject with TPNGImage in the code samples if you're working with TPNGImage.Carothers
O
3

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.

Using IWICBitmapScaler, you can scale down or up the image.

WIC is available on Windows Vista, and Windows 7. For Windows XP, you have to install an update before using it.

Obscenity answered 13/3, 2010 at 16:42 Comment(0)
B
1

There are versions of PngImage (Portable Network Graphics Delphi) That allow to do it via simple StretchDraw.

I have version which allows to do it - 1.564 (31 July 2006)

And version which not allows - 1.4361 (8 March 2003)

To perform it I used:

heart.png - with transparency and I managed to resize it and save with transparency.

empty.png - the pure transparent 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

Brownell answered 16/5, 2013 at 2:30 Comment(0)
C
1

Unfortunately, the vcldeveloper did not give an example in his answer.

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.

Castano answered 1/5, 2020 at 9:9 Comment(0)
M
-1

I must confess that I don't have any experience in programmatically play with 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).

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

Marengo answered 13/3, 2010 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.