Life-time learner's blog

[My old trash] Resizing images in Delphi.


A hundred years ago I was working on a PHP web site. I needed to place there a lot of images but all of them have different resolutions and sizes. For example, pictures from photo camera were very heavy (~ 5 Mb) and maximum file size that was allowed on the hosting was 3 Mb. So, before placing those photos to my hosting I needed to scale them down so that they met the limits.

You might have suggested using a PhotoShop or anything like that to get this job done. But I didn’t have the money to use this software (actually, I still don’t have). There were also a lot of free photo editors, which provided the function to scale images in bulk, but they were too overloaded and not so handy. Besides, I was very curious about how to make such a tool that would solve my problem.

Here I’m going to show you how to create a tool in Delphi that will automatically scale all images (only jpeg) in the directory that you specify. Why Delphi? Simply because it was the only programming language I was good enough at.

To begin with I’d like to show you a function that I found on the Internet. Unfortunately, I don’t remember where and I can’t name its author. Any way, I left the code unchanged.

Function that scales JPEG image in Delphi:

function ResampleJPEG(const inFile, outFile: TFileName;
  const aQuality: TJPEGQualityRange = 95;
  const NewWidth: Integer = 0; const NewHeight: Integer = 0): Boolean;
  BMP: TBitmap;
  Result:= FileExists(inFile);
  if not Result then

  JPEG:= TJPEGImage.Create;
  BMP:= TBitmap.Create;
      // Load
      Result:= False;
    if (NewWidth = 0) then
      BMP.Width:= Round(JPEG.Width*(NewHeight/JPEG.Height))
      BMP.Width:= NewWidth;
    if (NewHeight = 0) then
      BMP.Height:= Round(JPEG.Height*(NewWidth/JPEG.Width))
      BMP.Height:= NewHeight;
    BMP.PixelFormat:= pf24bit;
    // Change size
    with BMP.Canvas do
      StretchDraw(ClipRect, JPEG);
    // Move from Bitmap to Jpeg
    // Change quality
    JPEG.CompressionQuality:= aQuality;
      // Save file
      Result:= False;

Its input parameters are very obvious and I think there is nothing to explain. To compile this function you must include the module Jpeg to your uses list.

This function scales a single image, but hat if we want to scale all images in a directory? Well, it’s not a difficult problem. You just need to enumerate all jpeg files in the directory and apply the function to each file.

Functions that scales all images in the directory:

procedure scaleDirectory(const srcDir : String; const dstDir: String;
			 const aQuality: TJPEGQualityRange;
			 const newWidth: Integer; const newHeight: Integer);
  if FindFirst(srcDir+'/*.jpg',faAnyFile,R)<>INVALID_HANDLE_VALUE then
      ResampleJPEG(srcDir+'/'+R.Name,dstDir+'/'+R.Name, aQuality, newWidth, newHeight);
    until FindNext(R)<>0;

All images are saved into destination directory specified in dstDir.

We can decorate our programme using all means of Delphi IDE using its UI designer. Here is what I created:

It was very useful for me. Hopefully it is useful for you too.

Thanks for visiting. Good luck with your programming!

Anton Danshin


11 responses to “[My old trash] Resizing images in Delphi.

  1. Kevin Xi March 6, 2012 at 06:07

    Nice article! Now you are switching to Ubuntu, I believe you have tried Gimp already, things get easier:)

    • jetcracker March 6, 2012 at 14:16

      Yes. now I most of the time use ubuntu.
      By the way, I wrote this app when I was at school 4 years ago 🙂 At that time, I really had no idea that there were such a great OS – Ubuntu.

      • Kevin Xi March 6, 2012 at 17:19

        Yes, Ubuntu is great, I didn’t use Linux for a long time, at my college time, I used Archlinux more, it is also a nice distribution:)

  2. jetcracker March 6, 2012 at 20:38

    Why did you choose Windows and C++/C#?

  3. Pingback: Spring 2012: Overall stats « JetCracker

  4. carlos December 30, 2012 at 04:33

    Great post Anton,
    I found other page about Delphi with other tips about images that it can be useful for you (scale image preserve aspect ratio)

    • jetcracker December 30, 2012 at 07:45

      Thank you for your research 🙂
      But now I no longer use Delphi… It’s dead language, you know…
      I am a Java web developer now. Java is a very powerful and promising language!
      Since there is a huge rise of mobile devices and stuff, I am thinking of taking up programming for Android.

  5. siarik April 29, 2013 at 08:20

    May i request your program or full script, i really need it.
    thx before.

  6. siarikari April 29, 2013 at 15:21

    may I request your full code, I need it

    • jetcracker April 29, 2013 at 20:29

      Here it is:

      • siarik April 30, 2013 at 16:32

        thx u very much mr jetcracker 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: