Click here to Skip to main content
15,883,809 members
Articles / Programming Languages / Visual Basic
Article

Image Batch Resizer

Rate me:
Please Sign up or sign in to vote.
4.78/5 (62 votes)
26 Sep 20042 min read 284.4K   4.7K   138   64
A simple utility to quickly resize images

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:

VB.NET
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:

VB.NET
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


Written By
Technical Lead
Italy Italy
I was born in 1970.

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.

I graduated in Electronic Engineering and earned the following Microsoft certifications:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

I worked in IT as a developer, a teacher, a consultant, a technical writer, a technical leader.
IT knowledge applied to real life is my primary interest and focus.

Comments and Discussions

 
AnswerRe: Resize snippet question Pin
Alberto Venditti10-Nov-06 4:30
Alberto Venditti10-Nov-06 4:30 
GeneralRe: Resize snippet question Pin
Zayets22-Nov-06 18:29
Zayets22-Nov-06 18:29 
GeneralRe: Resize snippet question Pin
Alberto Venditti22-Nov-06 23:56
Alberto Venditti22-Nov-06 23:56 
GeneralThank you Pin
Shailen Sukul19-Aug-06 15:09
Shailen Sukul19-Aug-06 15:09 
QuestionSource DataStream is corrupt Pin
luckydyno8-Aug-06 19:16
luckydyno8-Aug-06 19:16 
AnswerRe: Source DataStream is corrupt Pin
Alberto Venditti9-Aug-06 2:19
Alberto Venditti9-Aug-06 2:19 
GeneralGood Job Pin
Tebby6-Apr-06 0:46
Tebby6-Apr-06 0:46 
QuestionResize with Resampling, for better quality? Pin
Mr_Analogy1-Feb-06 20:40
Mr_Analogy1-Feb-06 20:40 
If you resample the image when you resize it, you get a far better quality image. (I think this is similar to antialiasing with fonts).

(What is *looks* like when you resample something is that you get a "softer" less "jagged" looking image. This is most noticeable if you resize by a large factor (.1 or 10x, etc.)

I am looking for a way to resize with resampling.

Clay
AnswerRe: Resize with Resampling, for better quality? Pin
Alberto Venditti3-Feb-06 2:29
Alberto Venditti3-Feb-06 2:29 
GeneralResize pdf files Pin
Triskal17-May-05 8:30
Triskal17-May-05 8:30 
GeneralRe: Resize pdf files Pin
Alberto Venditti17-May-05 9:47
Alberto Venditti17-May-05 9:47 
GeneralRe: Resize pdf files Pin
preetraj17-Jul-06 21:36
preetraj17-Jul-06 21:36 
GeneralRe: Resize pdf files Pin
preetraj17-Jul-06 21:30
preetraj17-Jul-06 21:30 
GeneralAsk for GIS software Pin
nobita3x13-Mar-05 16:11
nobita3x13-Mar-05 16:11 
GeneralRe: Ask for GIS software Pin
Alberto Venditti14-Mar-05 7:21
Alberto Venditti14-Mar-05 7:21 
GeneralError in saving file back to disk Pin
toffee_paul7-Mar-05 9:01
toffee_paul7-Mar-05 9:01 
GeneralRe: Error in saving file back to disk Pin
Alberto Venditti7-Mar-05 23:38
Alberto Venditti7-Mar-05 23:38 
GeneralRe: Error in saving file back to disk Pin
budbjames9-Jan-06 10:37
budbjames9-Jan-06 10:37 
GeneralReduce by fixed size Pin
toffee_paul2-Mar-05 23:25
toffee_paul2-Mar-05 23:25 
GeneralRe: cReduce by fixed size Pin
Alberto Venditti3-Mar-05 21:45
Alberto Venditti3-Mar-05 21:45 
GeneralRe: Reduce by fixed size Pin
toffee_paul7-Mar-05 8:50
toffee_paul7-Mar-05 8:50 
GeneralRe: Reduce by fixed size Pin
Alberto Venditti7-Mar-05 23:27
Alberto Venditti7-Mar-05 23:27 
GeneralRe: Reduce by fixed size Pin
Mambo073-May-06 14:35
Mambo073-May-06 14:35 
GeneralVery Nice Pin
Steve W Brookes19-Jan-05 16:09
Steve W Brookes19-Jan-05 16:09 
GeneralJust what I need ! Pin
Mauricio Ritter14-Nov-04 3:04
Mauricio Ritter14-Nov-04 3:04 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.