Jump to content
dmitrok2006

Resize PNG in Delphi 11

Recommended Posts

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  LPictureSource: TPicture;  // load a PNG
  LPictureTarget: TPicture;  // to resize like a Bitmap
  LPNGresulted  : TPngImage; // to save PNG resulted
  LBtmPointerTmp: TBitmap;
begin
  LBtmPointerTmp := TBitmap.Create(1, 1);
  LPictureSource := TPicture.Create;
  LPictureTarget := TPicture.Create;
  LPNGresulted   := TPngImage.Create;
  try
    LPictureSource.Assign(Image2.Picture);
    // LPictureSource.LoadFromFile('..\..\IronMan.png');
    LPictureSource.Graphic.Transparent := True;
    //
    Memo1.Text := 'IronMan.png = ' + LPictureSource.Width.ToString + 'x' + LPictureSource.Height.ToString;
    //
    LPictureTarget.Bitmap.Assign(LBtmPointerTmp); // Create(1, 1); // just for create intervalues values to canvas, etc...
    LPictureTarget.Graphic.Transparent := True;
    //
    // new size
    LPictureTarget.Bitmap.SetSize( Trunc(LPictureSource.Width * 0.4), Trunc(LPictureSource.Height * 0.4) );
    //
    // draw on new canvas resized
    LPictureTarget.Bitmap.Canvas.StretchDraw(LPictureTarget.Bitmap.Canvas.ClipRect, LPictureSource.Graphic);
    //
    Memo1.Lines.Add(LPictureTarget.Width.ToString + 'x' + LPictureTarget.Height.ToString);
    //
    LPNGresulted.Assign(LPictureTarget.Bitmap);
    //
    Memo1.Lines.Add('PNG = ' + LPNGresulted.Width.ToString + 'x' + LPNGresulted.Height.ToString);
    //
    LPNGresulted.SaveToFile('resulted.png'); // just for test load file...
    //
    Image1.Picture.LoadFromFile('resulted.png');
    Image1.Proportional := True;
  finally
    LPNGresulted.Free;
    LPictureTarget.Free;
    LPictureSource.Free;
    LBtmPointerTmp.Free;
  end;
end;

initialization

ReportMemoryLeaksOnShutdown := True;

finalization

end.

 

Edited by programmerdelphi2k

Share this post


Link to post

Just so you know, while all the code in this thread might be fine for "I'm a hobbyist and I don't know what the hell I'm doing"-level programming, it's nowhere near the best or correct way to solve your problem.

 

With that said, the reason you're losing transparency is that you are operating on the rendered visual representation of the image. Therefore the transparency has been replaced with a background color.

In order to resize a PNG without losing transparency you will need to use a method that supports alpha transparency. This means:

  1. Convert the PNG to a 32-bit RGBA bitmap.
  2. Resample (resize) this bitmap to the desired size.
  3. Convert the bitmap to PNG.

There are various libraries that can do these steps for you. For example Graphics32 and probably also Image32. I'm guessing Image32 will probably be the easiest for you to understand.

  • Like 2
  • Thanks 1

Share this post


Link to post

sorry Melander, as you know my knowledge is not near your, but it's the life!!! Luck for us "have someone like you" for fix our error!  :classic_cheerleader:

go ahead...

 

in my test, the "Ironman - result.png" have background transparent

 

image.thumb.png.7a78aedb5de7061e1e33938771a85aaf.png     chrome_OC78xNmrmx.gif

Edited by programmerdelphi2k
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×