Author Topic: Load / Save image with resize ?  (Read 2885 times)

Offline pka4916

  • Imaging User
  • *
  • Posts: 9
    • View Profile
Load / Save image with resize ?
« on: 5 January 2010, 16:00:39 »
I'm kinda confused

when I load a image into a Timage  and save it, then the file gets really big
the input is a jpg 250Kb  and the output a PNG  1.8 Mb
why?

This is what I used

Code: Delphi
  1. FImage := TSingleImage.Create;
  2. ConvertBitmapToImage(Timage1Picture.Bitmap, FImage);
  3. FImage.SaveToFile('c:\test.png');
  4.  

I must say, that I nowhere set something that it  needs to be a PNG file.
maybe that's it... but where do I need to put it and what?

also  the image =  1200x800
i am loading it into the Timage  at  320x240  and have it set to fix so i can see the whole thing.

how can i save it with that sizeformat?   now when I save it's still 1200x800

thank you very much
« Last Edit: 20 January 2010, 22:18:37 by Galfar »

Offline Galfar

  • Administrator
  • Imaging User
  • *****
  • Posts: 312
    • View Profile
    • Galfar's Homepage
Re: Load / Save image with resize ?
« Reply #1 on: 6 January 2010, 17:29:37 »
JPEG uses lossy compression - it throws away some image information away and it's never going back.
PNG on the other hand uses lossless compressions - so no information is lost during compression.
So It's to be expected that the same image in PNG file would take up more disk space than JPEG.

TImage just displays assigned TGraphic/TBitmap, it doesn't modify it in any way. You need to resize the bitmap manualy (from your code sample - either Timage1Picture.Bitmap.SetSize or FImage.Resize).

Offline pka4916

  • Imaging User
  • *
  • Posts: 9
    • View Profile
Re: Load / Save image with resize ?
« Reply #2 on: 6 January 2010, 17:44:38 »
if i use the Timage1Picture.Bitmap.SetSize  then it just grabs that piece from the picture (org size 1280x1800).  even though it shows the full pic inside the Timage.

Tried also the FImage.Resize  but then the original Aspect Ratio is not correct anymore.

Offline Galfar

  • Administrator
  • Imaging User
  • *****
  • Posts: 312
    • View Profile
    • Galfar's Homepage
Re: Load / Save image with resize ?
« Reply #3 on: 6 January 2010, 17:53:06 »
Just keep the aspect ratio in mind when resizing.

Code: [Select]
aspectratio := FImage.width / FImage.height; 
FImage.Resize(img, newwidth, newwidth / aspectratio, rfBicubic); 

Offline pka4916

  • Imaging User
  • *
  • Posts: 9
    • View Profile
Re: Load / Save image with resize ?
« Reply #4 on: 6 January 2010, 18:29:55 »
Delphi doesn't take that.     Extended vs Integer
Did a Round( )    but then the ratio = 1  so  ain't right.

Offline Galfar

  • Administrator
  • Imaging User
  • *****
  • Posts: 312
    • View Profile
    • Galfar's Homepage
Re: Load / Save image with resize ?
« Reply #5 on: 6 January 2010, 19:05:42 »
Do the Round on newwidth / aspectratio, not on FImage.width / FImage.height.
Aspectratio variable should be real number.

Code: [Select]
var aspectratio: Single;
aspectratio := FImage.width / FImage.height; 
FImage.Resize(img, newwidth, Round(newwidth / aspectratio), rfBicubic);