ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 resize images
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

Kostas

Germany
21 Posts

Posted - Jun 28 2025 :  05:38:07  Show Profile  Reply
I have several .png images with different file sizes.
Is there a way to automatically resize the images to a maximum size?

regards,
Kostas

xequte

39076 Posts

Posted - Jun 28 2025 :  20:50:27  Show Profile  Reply
Hi Kostas

You can resize images using the Resample() method:

http://www.imageen.com/help/TImageEnProc.Resample.html


Have you tried our batch processing demo?

\Demos\InputOutput\BatchConvert\BatchConvert.dpr

Nigel
Xequte Software
www.imageen.com
Go to Top of Page

Kostas

Germany
21 Posts

Posted - Jul 02 2025 :  07:44:45  Show Profile  Reply
Hi Nigel,

sorry for the misunderstanding.
I don't want to reduce the image size in pixels, but rather the file size in KB.
All images should be a maximum of 400KB in size. I don't care about the resolution in pixels.

regards,
Kostas
Go to Top of Page

xequte

39076 Posts

Posted - Jul 02 2025 :  21:49:40  Show Profile  Reply
Hi Kostas

There are multiple ways to reduce the file size of an image:

- Save to a more compressed format: If your images are PNG, then the easiest way to reduce file size is to save to lossy format like JPEG (if you don't need transparency) or WebP (if you need transparency)
- Improve compression: Many formats support compression options, including PNG, optimize these: http://www.imageen.com/help/TIOParams.html
- Resample the image to make it smaller (e.g. reducing the size of an image by 50% will make it around 75% smaller on disk)
- Reduce the colors: Generally not a recommended method


If you have a strict maximum size (e.g. due to database constraints) and you are using a format that supports compression (so cannot quickly calculate the final file size), then you will need to create a method that saves a temporary file (you should just save to a TMemoryStream for speed) and keeps optimizing the options above until it does not exceed the maximum size, e.g.

// Keep reducing the quality of the image until it matches our size constraint
jpegQ := 95;
sz := MaxInt;
While sz > Maximum_Size do
begin
  BMP.Params.JPEG_Quality := jpegQ;
  BMP.SaveToStream( ms, ioJPEG );
  sz := ms.Size;
  dec( jpegQ, 5 );
  if jpegQ < 10 ) then
    raise EException.create( 'cannot optimize this image enough' );
end;

// Do something with ms, e.g. output to file or database


Nigel
Xequte Software
www.imageen.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: