Jump to content
dmitrok2006

Resize PNG in Delphi 11

Recommended Posts

Hello! Need help on this topic. In general, I will describe in more detail. I throw Image1 on the form. Manually (just manually) I load a Png image there, say 600x700, with transparency. Using the code, I need to resize this image loaded in Image1 and then save it to disk in new sizes. The Stretch property doesn't help, because on the contrary, Image1 is adjusted to fit the image and as a result, when saved, Png of the same sizes. I tried Image1.Picture.graphic.setsize, but then a piece of the picture is saved

Help. pliz......

Share this post


Link to post

I think that is VCL, not?  Image1.Picture.Graphic.SetSize(...) ...

procedure TForm1.Button1Click(Sender: TObject);
var
  LPictureSource: TPicture;  // load a PNG
  LPictureTarget: TPicture;  // to resize like a Bitmap
  LPNGresulted  : TPngImage; // to save PNG resulted
begin
  LPictureSource := TPicture.Create;
  LPictureTarget := TPicture.Create;
  LPNGresulted   := TPngImage.Create;
  try
    LPictureSource.LoadFromFile('Spiderman.png'); // with transparent background on png
    LPictureSource.Graphic.Transparent := True;
    //
    Memo1.Text := 'Spiderman.png = ' + LPictureSource.Width.ToString + 'x' + LPictureSource.Height.ToString;
    //
    LPictureTarget.Bitmap.Create(1, 1); // just for create internal values to canvas, etc...
    LPictureTarget.Graphic.Transparent := True;
    //
    // new size
    LPictureTarget.Bitmap.Width  := Trunc(LPictureSource.Width * 4.0); // size x 4
    LPictureTarget.Bitmap.Height := Trunc(LPictureSource.Height * 4.0);
    //
    // 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;
  end;
end;

image.thumb.png.14efa0aba2d9963e0dd266a40670e321.pngimage.thumb.png.6c0874d6694967b963b9d418d2031f74.png     image.thumb.png.60c22084226af83ca2608cd93df29b14.png

Edited by programmerdelphi2k
  • Like 1

Share this post


Link to post

programmerdelphi2k, I want to ask ....
Maybe I don't understand something...
But the code uses LoadFromFile, and I need to load the image manually, without using LoadFromFile.

Share this post


Link to post

I'm fine with any boot method that doesn't access the disk while the exe file is running...
For example, in my project, I manually loaded PNG into Image1....

Share this post


Link to post
11 hours ago, programmerdelphi2k said:

// draw on new canvas resized

  LPictureTarget.Bitmap.Canvas.StretchDraw(LPictureTarget.Bitmap.Canvas.ClipRect, LPictureSource.Graphic);

after this line, "LPictureSource" it's not necessary anymore, then, you can " LPictureSource.Graphic := nil"

  • Like 1

Share this post


Link to post
58 minutes ago, programmerdelphi2k said:

@dmitrok2006

you need "load" your image using any way... file, stream, resource, etc...! you decide how to?

On Form1 I don't need to put Image1 ?

Share this post


Link to post

To tell the truth, you would only need to embed a "resource" (image, sound, etc...) in strictly necessary case, where to load it... would be something very time consuming, or if the resource doesn't cost much for your system (memory, performance, etc...)!
Overall you could just load it "on-demand"

 

Using a component is just to make the job easier, in fact you can create it at run-time. Or, if you don't need a component, you can just create the component class to do your background work... in code!

Edited by programmerdelphi2k
  • Like 1

Share this post


Link to post
var
  MyImage: TImage;
begin
  MyImage := TImage.Create(nil);
  try
    MyImage.Left  := 0; // any values ...
    MyImage.Top   := 0;
    MyImage.Width := 0;
    // ...
    MyImage.Picture.LoadFromFile('....');
    //
    MyImage.Parent := self; // self = form1 -> "parent" to appears on screen of form area!!
  finally
    MyImage.Free;
  end;

 

Edited by programmerdelphi2k
  • Like 1

Share this post


Link to post

Programmerdelphi2k...

One more question....
I made this code, but the transparency seems to have disappeared in the saved PNG. Why?

var
  Form1: TForm1;
  PNGSource: TPicture;
  PNGTarget: TPicture;
  PNGresult: TPngImage;
implementation

{$R *.dfm}

uses Unit2;

procedure TForm1.a0Click(Sender: TObject);
begin
  PNGSource:= TPicture.Create;
  PNGTarget:= TPicture.Create;
  PNGresult:= TPngImage.Create;

  PNGSource:=form2.Aa.Picture;
  PNGSource.Graphic.Transparent := True;

  PNGTarget.Bitmap.Width  := Trunc(PNGSource.Width / 4.0);
  PNGTarget.Bitmap.Height := Trunc(PNGSource.Height / 4.0);

  PNGTarget.Bitmap.Canvas.StretchDraw(PNGTarget.Bitmap.Canvas.ClipRect, PNGSource.Graphic);

  PNGresult.Assign(PNGTarget.Bitmap);
  PNGresult.SaveToFile('c:\1DU\As.png');

 

Share this post


Link to post

a little fix to avoid a "memory leaks"...

Quote

replace this


LPictureTarget.Bitmap.Create(1, 1); // just for create internal values to canvas, etc...   = memory leaks on end-app!!!

with a new obj TBitmap as usual

Quote

BmpTmp := TBitmap.Create(1,1);

...

LPictureTarget.Bitmap.Assign(BmpTmp);

...

BmpTmp.free; // when it's not more necessary!

 

  • Like 1

Share this post


Link to post

Programmerdelphi2k...   Wow, such a big code)))) I need a few hours to understand. So I'll write the answer tomorrow, ok?
Thank you, you are a good person )

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

×