Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Resizing Batch Images

, 21 Feb 2008 CPOL
Resize all images in the source folder and save them in the destination folder
resizebatchimage.zip
resizeBatchImage
resizeBatchImage.suo
resizeBatchImage
bin
Release
My Project
Application.myapp
Settings.settings
Public Class frmMain
    ' ------------------------------------------------------------
    ' Programmer : Reza Shirazi Mofrad 
    ' 
    ' homepage : www.rezashirazi.com
    ' ------------------------------------------------------------

    Private Sub lnkAbout_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkAbout.LinkClicked
        System.Diagnostics.Process.Start("http://www.rezashirazi.com")
    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Dispose()
    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
        FolderBrowserDialog1.Dispose()
    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
        FolderBrowserDialog1.Dispose()
    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
                    Else
                        ' 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))
            Else
                BackgroundWorker1.ReportProgress(100)
            End If
        Next

    End Sub

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        lblPleasewait.Visible = True
        BackgroundWorker1.RunWorkerAsync()
    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
        Application.DoEvents()
    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.

License

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

Share

About the Author

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

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