Click here to Skip to main content
15,884,739 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

 
GeneralMy vote of 5 Pin
Member 1022520316-Sep-13 2:31
professionalMember 1022520316-Sep-13 2:31 
Questionsuperb Pin
george nepolian4-Mar-12 23:35
george nepolian4-Mar-12 23:35 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey21-Feb-12 22:06
professionalManoj Kumar Choubey21-Feb-12 22:06 
GeneralReally useful! Pin
Al-Benj7-Dec-11 5:56
Al-Benj7-Dec-11 5:56 
GeneralExcellent..! Pin
jdpatel02323-Nov-11 20:51
jdpatel02323-Nov-11 20:51 
GeneralMy vote of 5 Pin
ankitj3120-Jul-11 5:08
ankitj3120-Jul-11 5:08 
GeneralThank you! Pin
AKB6-Mar-11 11:45
AKB6-Mar-11 11:45 
QuestionRecurse sub-directories? Pin
robbie19731-Aug-10 0:54
robbie19731-Aug-10 0:54 
AnswerRe: Recurse sub-directories? Pin
Alberto Venditti3-Aug-10 5:34
Alberto Venditti3-Aug-10 5:34 
What you basically need is to repeat the computations done inside the btnGo_Click() event handler (that apply to a single folder) for any subfolder found in the initial input path, then recursively call the same modified procedure for all nodes of the subfolder structure.

Conceptually, in pseudocode you need to change this:

Private Sub btnGo_Click(...)
  Do something on txtINpath.Text
End Sub


into this:

Private Sub btnGo_Click(...)
  Call ScanDir(txtINpath.Text)
End Sub

Private Sub ScanDir(Path)
  Do something on Path

  For each subdirectory of Path
    Call ScanDir(a subdir of Path)
  Next



By translating this into practice, the following code should do the work (I did't test it yet... it's up to you):

Private Sub btnGo_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles btnGo.Click
  If Not Directory.Exists(txtINpath.Text) Or _
    Not Directory.Exists(txtOUTpath.Text) Then
    MessageBox.Show("The folder...", "Error", _
      MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Exit Sub
  End If

  ScanDir(txtINpath.Text)
End Sub


Private Sub ScanDir(ByVal Path As String)
  Dim fs As String() = Directory.GetFiles(Path, "*.jpg")
  Dim Ffull, Fshort As String
  For Each Ffull In fs
    FromFile(Ffull)
    Application.DoEvents()
    Fshort = Ffull.Substring(Ffull.LastIndexOf("\") + 1)
    lblName.Text = Fshort
    Application.DoEvents()
    Dim dr As DialogResult
    If chkBatchProc.Checked Then
      dr = DialogResult.Yes
    Else
      dr = MessageBox.Show("Convert?", Fshort, _
        MessageBoxButtons.YesNoCancel)
    End If
    If dr = DialogResult.Cancel Then
      Exit For
    ElseIf dr = DialogResult.Yes Then
      Reduce(Double.Parse(txtRedFactor.Text, _
        New System.Globalization.CultureInfo("EN-us")))
      Application.DoEvents()
      ToFile(txtOUTpath.Text & "\" & Fshort)
    End If
  Next

  Dim f As String
  ' Scan all the subdirectories, call recursively Scandir
  Dim paths As String() = Directory.GetDirectories(Path)
  For Each f In paths
    ScanDir(f)
  Next

End Sub


Of course, in this example, the output path will be the same for all the generated pictures (resulting in a flattened folder structure in the generated output).
If this is not the desired behavior, and you need to preserve in the output result the folder structure of the source input pictures tree, you will have to modify the call to "ToFile(...)" in order to use a destination folder that contains the desired substring path of the input folder.

Hope this helps.
Cheers, AV
GeneralRe: Recurse sub-directories? Pin
robbie19733-Aug-14 2:50
robbie19733-Aug-14 2:50 
GeneralThanks Pin
hardikdarji19-Feb-10 3:24
hardikdarji19-Feb-10 3:24 
GeneralNeed Documentation Pin
Abdul Zamrood11-Jan-10 9:20
Abdul Zamrood11-Jan-10 9:20 
GeneralRe: Need Documentation Pin
Alberto Venditti11-Jan-10 20:48
Alberto Venditti11-Jan-10 20:48 
GeneralRe: Need Documentation Pin
Abdul Zamrood11-Jan-10 22:17
Abdul Zamrood11-Jan-10 22:17 
Generalproportions Pin
mardav11130-Jun-09 5:59
mardav11130-Jun-09 5:59 
GeneralRe: proportions Pin
Alberto Venditti30-Jun-09 22:26
Alberto Venditti30-Jun-09 22:26 
GeneralRe: proportions Pin
mardav1111-Jul-09 9:04
mardav1111-Jul-09 9:04 
GeneralRe: proportions Pin
Alberto Venditti1-Jul-09 22:04
Alberto Venditti1-Jul-09 22:04 
Generalout of memory Pin
sisprog28-Apr-09 11:05
sisprog28-Apr-09 11:05 
GeneralRe: out of memory Pin
Alberto Venditti28-Apr-09 23:32
Alberto Venditti28-Apr-09 23:32 
GeneralRe: out of memory Pin
sisprog29-Apr-09 2:46
sisprog29-Apr-09 2:46 
GeneralRe: out of memory Pin
Alberto Venditti29-Apr-09 21:07
Alberto Venditti29-Apr-09 21:07 
QuestionFfull?? [modified] Pin
juunas22112-Jul-08 4:59
juunas22112-Jul-08 4:59 
AnswerRe: Ffull?? Pin
Alberto Venditti2-Jul-08 5:35
Alberto Venditti2-Jul-08 5:35 
GeneralSimilar half-picture as luckydyno was having... Pin
buffbuh12-Jun-08 19:53
buffbuh12-Jun-08 19:53 

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.