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

zig

After looking for a way to resize TPngObject and maintain the transparency + alpha channels to no avail, I'm trying to use GDI+

Here is my code, and it seems to work fine. it will down/up scale a PNG. Tested on XP so far:

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;

My question: is using GetThumbnailImage the correct way of doing this? I did not find any other method.

TLama

I don't think that GetThumbnailImage method is a good way to go because I doubt that you will get a high quality resampled image. In this article you can find how to rescale the image. They're using the DrawImage method for that, so I would do the same. Just before that I would set also the high quality graphics modes to get high quality output. Here is an example:

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ffmpeg resize PNG image sequence using alpha padding

From

Golang - Swap PNG channels of a picture using Image and Image/PNG

From Dev

How to resize a tiff image with multiple channels?

From Dev

How to tint a png image with an alpha channel?

From Dev

How to open an image with alpha channels in OpenCV-Python?

From Dev

How to set alpha value for all pixels in a bitmap using MFC or GDI or GDI+

From Dev

After image resize transparency is removed - gdi+

From Dev

How to resize image using Codeigniter

From Dev

How to resize an image using libpng?

From Dev

C++ gdi::Bitmap to PNG Image in memory

From Dev

How do I place an image (.png) within a `LabelFrame`, and resize it, in Tkinter?

From Dev

Resize and Save Image in the Picturebox as PNG

From Dev

Is it possible to compress alpha and RGB channels of PNG sequence separately with FFmpeg?

From Dev

Read PNG image from stream with 4 channels

From Dev

How to resize image data using numpy?

From Dev

how to resize image in memory using convert command

From Java

How can I resize an image using Java?

From Dev

How to resize an image using Lumia Imaging SDK?

From Dev

How to resize and rotate image simulataneously using canvas

From Dev

Julia - How to resize image using function `imshow`?

From Dev

How to resize an image in Python without using Pillow

From Dev

How to resize image using Gd library ? PHP

From Dev

Python: How to resize an image using PIL module

From Dev

How to Compress Image but not resize it Using Xamarin IOS?

From Dev

How do I resize this image using tkinter?

From Dev

How to resize image using Pillow and tkinter

From Dev

GDI+ System.Runtime.InteropServices.ExternalException On Image resize and conversion

From Dev

PIL not always using 3 channels for PNG

From Dev

How to replace color of PNG image using CSS?

Related Related

  1. 1

    ffmpeg resize PNG image sequence using alpha padding

  2. 2

    Golang - Swap PNG channels of a picture using Image and Image/PNG

  3. 3

    How to resize a tiff image with multiple channels?

  4. 4

    How to tint a png image with an alpha channel?

  5. 5

    How to open an image with alpha channels in OpenCV-Python?

  6. 6

    How to set alpha value for all pixels in a bitmap using MFC or GDI or GDI+

  7. 7

    After image resize transparency is removed - gdi+

  8. 8

    How to resize image using Codeigniter

  9. 9

    How to resize an image using libpng?

  10. 10

    C++ gdi::Bitmap to PNG Image in memory

  11. 11

    How do I place an image (.png) within a `LabelFrame`, and resize it, in Tkinter?

  12. 12

    Resize and Save Image in the Picturebox as PNG

  13. 13

    Is it possible to compress alpha and RGB channels of PNG sequence separately with FFmpeg?

  14. 14

    Read PNG image from stream with 4 channels

  15. 15

    How to resize image data using numpy?

  16. 16

    how to resize image in memory using convert command

  17. 17

    How can I resize an image using Java?

  18. 18

    How to resize an image using Lumia Imaging SDK?

  19. 19

    How to resize and rotate image simulataneously using canvas

  20. 20

    Julia - How to resize image using function `imshow`?

  21. 21

    How to resize an image in Python without using Pillow

  22. 22

    How to resize image using Gd library ? PHP

  23. 23

    Python: How to resize an image using PIL module

  24. 24

    How to Compress Image but not resize it Using Xamarin IOS?

  25. 25

    How do I resize this image using tkinter?

  26. 26

    How to resize image using Pillow and tkinter

  27. 27

    GDI+ System.Runtime.InteropServices.ExternalException On Image resize and conversion

  28. 28

    PIL not always using 3 channels for PNG

  29. 29

    How to replace color of PNG image using CSS?

HotTag

Archive