Click here to Skip to main content
Click here to Skip to main content

Image Batch Resizer

By , 26 Sep 2004
 

Introduction

I came back from my holidays, with tons of photos taken with my new digital camera. Normally, I prefer to take pictures using the best quality and resolution, and this - of course - implies having bigger files to manage.

Often, at a good resolution, a single photo in JPG format takes more than 1 Mb, and this is not so comfortable if you want to send your pictures to your friends via email, or to put them on a web site. You have to reduce the quality of your pictures, resampling them at a lower resolution.

I had this need of resizing pictures and - even if the world is full of image processing software capable of doing that - I decided to write my little piece of code to do it in VB.NET.

How the utility works

The utility I wrote is very simple: given an "input" folder, it looks for JPG pictures in that folder and - one by one - it takes them, reduces their sizes by a specified factor, and save them in a given "output" folder.

Image Batch Resizer user interface

In the "normal mode", when you press the "GO" button, the input folder is scanned, each single picture found is shown in the preview box and you are asked about the conversion of that specific picture. Under the preview box you can find the filename and the size of the image (size on disk, dimensions X and Y, aspect ratio).

The conversion can be done also in "batch mode" (that is: without human intervention, converting all the pictures found in the input folder) if you check the "Batch processing" checkbox before pressing the "GO" button. To stop the batch processing while running, just uncheck the option to revert to "normal mode".

Something about the code

The core reducing functionality is inside the Reduce() subroutine, specifically in this line of code:

img = New Bitmap(img, New Size(img.Size.Width * factor, 
  img.Size.Height * factor))

Notice that the reducing factor is applied to both picture dimensions (to preserve the aspect ratio); so, if the original picture's size is [x,y] and the reducing factor is F, the resulting image is sized [x*F,y*F] (hence its area is reduced by F*F).

It's easy to modify this formula to obtain different resizing behaviors. For example, if the set of the original pictures is not homogeneous in the sizes, you could want to reduce them not by a fixed factor, but to a specified size (regardless the original size).

The Reduce() subroutine also contains some lines of code used to compute the file size of the resulting JPG picture. This computation is simply done looking at the size of a MemoryStream on which the image is saved in JPG format:

Dim SizeKb As String
Dim ms As New MemoryStream()
img.Save(ms, Imaging.ImageFormat.Jpeg)
SizeKb = (ms.Length \ 1024).ToString() & "Kb "

A last thing to notice in the Image Batch Resizer utility is the persistence of the configuration settings between working sessions, accomplished by the use of the ConfigOpt class (described in a previous article of mine).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Alberto Venditti
Technical Lead
Italy Italy
Member
I was born in 1970.
 
I studied Electronic Engineering (graduated in 1997).
 
Subsequently, I passed some Microsoft exams, and currently I'm certified as:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).
 
My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionsuperbmembergeorge nepolian4 Mar '12 - 23:35 
GeneralMy vote of 5membermanoj kumar choubey21 Feb '12 - 22:06 
GeneralReally useful!memberAl-Benj7 Dec '11 - 5:56 
GeneralExcellent..!memberjdpatel02323 Nov '11 - 20:51 
GeneralMy vote of 5memberankitj3120 Jul '11 - 5:08 
GeneralThank you!memberAKB6 Mar '11 - 11:45 
QuestionRecurse sub-directories?memberrobbie19731 Aug '10 - 0:54 
AnswerRe: Recurse sub-directories?memberAlberto Venditti3 Aug '10 - 5:34 
GeneralThanksmemberhardikdarji19 Feb '10 - 3:24 
GeneralNeed DocumentationmemberAbdul Zamrood11 Jan '10 - 9:20 
GeneralRe: Need DocumentationmemberAlberto Venditti11 Jan '10 - 20:48 
GeneralRe: Need DocumentationmemberAbdul Zamrood11 Jan '10 - 22:17 
Generalproportionsmembermardav11130 Jun '09 - 5:59 
GeneralRe: proportionsmemberAlberto Venditti30 Jun '09 - 22:26 
GeneralRe: proportionsmembermardav1111 Jul '09 - 9:04 
GeneralRe: proportionsmemberAlberto Venditti1 Jul '09 - 22:04 
Well,
 
to me your solution appears quite identical to the original code: you are just dividing Width and Height by a factor instead of multiplying them by a factor.
 
img = New Bitmap(img, New Size(img.Width / txtRedFactor.Text, img.Height / txtRedFactor.Text))
 
img = New Bitmap(img, New Size(img.Size.Width * factor, img.Size.Height * factor))
 
While your initial question was "set a definite Height and keep Width proportionate", which is different: it implies determining the factor by old and new Height.
 
Another quick note: I think your solution works only with integer number (and not with fractional numbers) just because you're doing math between numbers and strings (which is a quite disputable thing...), so VB.NET is trying to implicitly cast txtRedFactor.Text to a number.
 
Cheers, AV
Generalout of memorymembersisprog28 Apr '09 - 11:05 
GeneralRe: out of memorymemberAlberto Venditti28 Apr '09 - 23:32 
GeneralRe: out of memorymembersisprog29 Apr '09 - 2:46 
GeneralRe: out of memorymemberAlberto Venditti29 Apr '09 - 21:07 
QuestionFfull?? [modified]memberjuunas22112 Jul '08 - 4:59 
AnswerRe: Ffull??memberAlberto Venditti2 Jul '08 - 5:35 
GeneralSimilar half-picture as luckydyno was having...memberbuffbuh12 Jun '08 - 19:53 
GeneralRe: Similar half-picture as luckydyno was having...memberAlberto Venditti12 Jun '08 - 21:56 
GeneralGood idea - very small "improvement"memberplouvou21 May '08 - 0:52 
Generalcouldn't scroll down to select an item flowlayout panelmemberMember 27663242 May '08 - 11:25 
QuestionSize difference on web pagememberwlanier228 Nov '07 - 16:22 
AnswerRe: Size difference on web pagememberAlberto Venditti29 Nov '07 - 4:56 
GeneralRe: Size difference on web pagememberwlanier23 Dec '07 - 15:43 
GeneralThanksmemberJim Franklin10 Dec '06 - 6:27 
GeneralRe: ThanksmemberAlberto Venditti11 Dec '06 - 23:05 
GeneralRe: Thanksmemberacord12 Dec '06 - 9:30 
QuestionResize snippet questionmemberZayets8 Nov '06 - 18:37 
AnswerRe: Resize snippet questionmemberAlberto Venditti10 Nov '06 - 4:30 
GeneralRe: Resize snippet questionmemberZayets22 Nov '06 - 18:29 
GeneralRe: Resize snippet questionmemberAlberto Venditti22 Nov '06 - 23:56 
GeneralThank youmemberShailen Sukul19 Aug '06 - 15:09 
QuestionSource DataStream is corruptmemberluckydyno8 Aug '06 - 19:16 
AnswerRe: Source DataStream is corruptmemberAlberto Venditti9 Aug '06 - 2:19 
GeneralGood JobmemberTebby6 Apr '06 - 0:46 
QuestionResize with Resampling, for better quality?memberMr_Analogy1 Feb '06 - 20:40 
AnswerRe: Resize with Resampling, for better quality?memberAlberto Venditti3 Feb '06 - 2:29 
GeneralResize pdf filesmemberTriskal17 May '05 - 8:30 
GeneralRe: Resize pdf filesmemberAlberto Venditti17 May '05 - 9:47 
GeneralRe: Resize pdf filesmemberpreetraj17 Jul '06 - 21:36 
GeneralRe: Resize pdf filesmemberpreetraj17 Jul '06 - 21:30 
GeneralAsk for GIS softwaremembernobita3x13 Mar '05 - 16:11 
GeneralRe: Ask for GIS softwarememberAlberto Venditti14 Mar '05 - 7:21 
GeneralError in saving file back to diskmembertoffee_paul7 Mar '05 - 9:01 
GeneralRe: Error in saving file back to diskmemberAlberto Venditti7 Mar '05 - 23:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 27 Sep 2004
Article Copyright 2004 by Alberto Venditti
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid