- 342,499 hits
Monitor Your Heart
Top Posts & Pages
Error: Twitter did not respond. Please wait a few minutes and refresh this page.
Life-time learner's blog
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; var JPEG: TJPEGImage; BMP: TBitmap; begin Result:= FileExists(inFile); if not Result then Exit; JPEG:= TJPEGImage.Create; BMP:= TBitmap.Create; try try // Load JPEG.LoadFromFile(inFile); except Result:= False; Exit; end; if (NewWidth = 0) then BMP.Width:= Round(JPEG.Width*(NewHeight/JPEG.Height)) else BMP.Width:= NewWidth; if (NewHeight = 0) then BMP.Height:= Round(JPEG.Height*(NewWidth/JPEG.Width)) else BMP.Height:= NewHeight; BMP.PixelFormat:= pf24bit; // Change size with BMP.Canvas do StretchDraw(ClipRect, JPEG); // Move from Bitmap to Jpeg JPEG.Assign(BMP); // Change quality JPEG.CompressionQuality:= aQuality; JPEG.Compress; try // Save file JPEG.SaveToFile(outFile); except Result:= False; end; finally FreeAndNil(JPEG); FreeAndNil(BMP); end; end;
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); begin if FindFirst(srcDir+'/*.jpg',faAnyFile,R)<>INVALID_HANDLE_VALUE then repeat //Application.ProcessMessages; ResampleJPEG(srcDir+'/'+R.Name,dstDir+'/'+R.Name, aQuality, newWidth, newHeight); //sleep(10); until FindNext(R)<>0; findClose(R); end;
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!