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;
if not Result then
if (NewWidth = 0) then
if (NewHeight = 0) then
// Change size
with BMP.Canvas do
// Move from Bitmap to Jpeg
// Change quality
// Save file
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);
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!