Click here to Skip to main content
15,861,168 members
Articles / Programming Languages / Visual Basic

Resizing Batch Images

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
21 Feb 2008CPOL 33.1K   851   28   2
Resize all images in the source folder and save them in the destination folder
resizeBatchImage

Introduction

This application gets the source folder of images/photos in JPEG format and resizes them into a specific size and saves them into a destination directory.
The user can check crop setting for cropping images before resizing them.

Using the Code

VB.NET
Public Function ResizePicture(ByVal sourceImage As Bitmap, _
     ByVal newSize As Size) As Bitmap

     Dim Result_image As New Bitmap(sourceImage, newSize.Width, newSize.Height)
     Dim Gr As Graphics

     Gr = Graphics.FromImage(Result_image)
     Gr.DrawImage(Result_image, 0, 0, newSize.Width, newSize.Height)
     Gr.Save()

     Return Result_image
 End Function

 Public Function CropBitmap(ByVal inputBmp As Bitmap, _
     ByVal cropRectangle As Rectangle) As Bitmap
     'Create a new bitmap object based on the input
     Dim newBmp As New Bitmap(cropRectangle.Width, _
              cropRectangle.Height, _
              System.Drawing.Imaging.PixelFormat.Format24bppRgb) 'Graphics.FromImage
                     'doesn't like Indexed pixel format

     'Create a graphics object and attach it to the bitmap
     Dim newBmpGraphics As Graphics = Graphics.FromImage(newBmp)

     'Draw the portion of the input image in the crop rectangle
     'in the graphics object
     newBmpGraphics.DrawImage(inputBmp, _
           New Rectangle(0, 0, cropRectangle.Width, cropRectangle.Height), _
             cropRectangle, _
             GraphicsUnit.Pixel)

     'Return the bitmap
     newBmpGraphics.Dispose()

     'newBmp will have a RawFormat of MemoryBmp because it was created
     'from scratch instead of being based on inputBmp.  Since it is inconvenient
     'for the returned version of a bitmap to be of a different format, now convert
     'the scaled bitmap to the format of the source bitmap
     Return newBmp
 End Function

I wrote this application in about 30 minutes. This program is useful for batch resizing for a large number of photos.

History

  • 21st February, 2008: Initial post

License

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


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey7-Mar-12 5:33
professionalManoj Kumar Choubey7-Mar-12 5:33 
Generalkhalid altahree student from yemen Pin
Kh.Taheri26-Nov-08 2:52
Kh.Taheri26-Nov-08 2:52 

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.