Click here to Skip to main content
12,947,379 members (46,809 online)
Click here to Skip to main content

Tagged as


28 bookmarked
Posted 21 Feb 2008

Resizing Batch Images

, 21 Feb 2008 CPOL
Resize all images in the source folder and save them in the destination folder
Public Class frmMain
    ' ------------------------------------------------------------
    ' Programmer : Reza Shirazi Mofrad 
    ' homepage :
    ' ------------------------------------------------------------

    Private Sub lnkAbout_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkAbout.LinkClicked
    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
    End Sub

    Private Sub cmdBrowseImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowseImage.Click
        If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtImageFolder.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub cmdBrowseOutput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowseOutput.Click
        If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtOutput.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub resizeFolderImages(ByVal SourceFolder As String, _
        ByVal destinationFolder As String, _
        ByVal cropImages As Boolean, _
        ByVal width As Integer, _
        ByVal height As Integer)

        Dim cropRect As Rectangle
        Dim fileNames As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
        Dim i As Integer
        Dim mSourceImage As Bitmap
        Dim obj As New clsResize
        Dim fileName As String

        fileNames = My.Computer.FileSystem.GetFiles(SourceFolder, FileIO.SearchOption.SearchAllSubDirectories, "*.jpg")

        For i = 0 To fileNames.Count - 1
            mSourceImage = Image.FromFile(fileNames(i).ToString())
            fileName = destinationFolder & "\" & fileNames(i).ToString().Substring(fileNames(i).ToString().LastIndexOf("\") + 1)

            ' check if croping image is enable in settings 
            If cropImages Then
                ' crop image ... 
                If (mSourceImage.Width / mSourceImage.Height) _
                    <> (width / height) Then

                    If (mSourceImage.Width / mSourceImage.Height) _
                        > (width / height) Then

                        ' minimum is height 
                        cropRect.Height = mSourceImage.Height
                        cropRect.Width = mSourceImage.Height * width / height
                        ' minimum is width
                        cropRect.Width = mSourceImage.Width
                        cropRect.Height = mSourceImage.Width * height / width
                    End If
                    cropRect.X = Math.Abs(mSourceImage.Width - cropRect.Width) / 2
                    cropRect.Y = Math.Abs(mSourceImage.Height - cropRect.Height) / 2

                    mSourceImage = obj.CropBitmap(mSourceImage, cropRect)
                End If
            End If

            ' save resized image to destination Folder
            obj.ResizePicture(mSourceImage, New Size(width, height)).Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg)

            ' set progress value
            If fileNames.Count > 1 Then
                BackgroundWorker1.ReportProgress(CInt(CDbl(i / (fileNames.Count - 1)) * 100))
            End If

    End Sub

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        lblPleasewait.Visible = True
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        resizeFolderImages(txtImageFolder.Text.Trim, _
            txtOutput.Text.Trim, _
            chkEnableCrop.Checked, _
            txtWidth.Value, txtHeight.Value)
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        lblPleasewait.Visible = False
        MsgBox("Operation is completed.")
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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


About the Author

reza shirazi
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170518.1 | Last Updated 21 Feb 2008
Article Copyright 2008 by reza shirazi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid