Click here to Skip to main content
15,881,204 members
Articles / Desktop Programming / Windows Forms

Batch Image Resizer

Rate me:
Please Sign up or sign in to vote.
4.62/5 (12 votes)
9 Jun 2011CPOL1 min read 32.8K   2.9K   22   6
Resize batch or many images at once, just drag and drop files and specify height and width

Sample Image - maximum width is 600 pixels

Introduction

Batch Image Resizer resizes all the selected images to a particular size as specified in the textbox. To resize several images at once, just drag and drop the files to the left box (Left box accepts only image files), specify the height and width to which you want to resize them and click on the resize button. Then, it will show all the resized images in the right box. Images checked in the right box are resized successfully and images that are unchecked are not successfully resized due to some error.

Using the Code

Given below is the main source code...

The below subroutine resizes images of different formats, and keeps their format as it is.

VB.NET
Private Sub BatchImageResizerfun(ByVal strr As String)

        Dim bm As New Bitmap(strr)
        Dim i As Integer

        Dim str11 As String = Mid(strr, Len(strr) - 2, 3) 'Stores the format 
						'of the image i.e. png, jpg

        Dim bmname As String = ""
        Dim c As Char = Nothing

        For i = 4 To Len(strr) 'stores  filename
            c = Mid(strr, Len(strr) - i, 1)
            If c = Char.Parse("\") Then
                Exit For
            End If
            bmname = bmname + c
        Next

        bmname = mypicturefolder & "\" & StrReverse(bmname)

        Dim width As Integer = Integer.Parse(TextBox2.Text)	'image width.
        Dim height As Integer = Integer.Parse(TextBox1.Text)	'image height

        Dim thumb As New Bitmap(width, height)
        Dim g As Graphics = Graphics.FromImage(thumb)

        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.DrawImage(bm, New Rectangle(0, 0, width, height), _
		New Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel)
        g.Dispose()

        Try
            Select Case Strings.LCase(str11) 'saves the file to their 
					'corresponding format after resize
                Case ""
                    Exit Sub
                Case "bmp"
                    thumb.Save(bmname & ".bmp", Imaging.ImageFormat.Bmp)
                Case "jpg"
                    thumb.Save(bmname & ".jpg", Imaging.ImageFormat.Jpeg)
                Case "gif"
                    thumb.Save(bmname & ".gif", Imaging.ImageFormat.Gif)
                Case "ico"
                    thumb.Save(bmname & ".ico", Imaging.ImageFormat.Icon)
                Case "png"
                    thumb.Save(bmname & ".png", Imaging.ImageFormat.Png)
                Case "tiff"
                    thumb.Save(bmname & ".tiff", Imaging.ImageFormat.Tiff)
                Case "wmf"
                    thumb.Save(bmname & ".wmf", Imaging.ImageFormat.Wmf)
            End Select
            CheckedListBox1.Items.Add(bmname & "." & str11, True) 'file resized 
							'successfully
        Catch ex As Exception
            CheckedListBox1.Items.Add(bmname & "." & str11, False) 'file resizing 
							'unsuccessful
        End Try

        bm.Dispose()
        thumb.Dispose()

    End Sub

When images are dropped to the left box from the explorer, it checks for the following:

  1. It is an image file or not. If not, then don't add to the list.
  2. If the image file is already added to the list, then don't add it again.
    To remove pictures from being resized, select the file and right click to remove from the list.

To resize pictures, specify width and height to be resized, and then click on resize button. It resizes pictures and opens the directory where the pictures have been stored.

The below image shows how it works:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionvery good Pin
Southmountain28-Dec-19 17:37
Southmountain28-Dec-19 17:37 
QuestionMy vote 5 Pin
Steven Sky25-Jun-13 21:38
Steven Sky25-Jun-13 21:38 
GeneralUse methods from System.IO.Path to parse path Pin
maskrtnik0112-Jun-11 5:30
maskrtnik0112-Jun-11 5:30 
GeneralMy vote of 4 Pin
VallarasuS9-Jun-11 7:56
VallarasuS9-Jun-11 7:56 
GeneralMy vote of 3 Pin
bomt9-Jun-11 1:59
bomt9-Jun-11 1:59 
GeneralRe: My vote of 3 Pin
thatraja12-Jul-11 15:51
professionalthatraja12-Jul-11 15:51 

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.