Click here to Skip to main content
15,891,865 members
Articles / Programming Languages / Visual Basic

An Easy Way to Resize an Image

Rate me:
Please Sign up or sign in to vote.
4.44/5 (28 votes)
9 Jun 2009CPOL2 min read 60.4K   1.8K   43  
This article will show you how to resize an image keeping with the best graphics quality.
Imports System.IO
Public Class frmResize
    Public strSource As String
    Public strDestination As String

 
    Public Function ImageResize(ByVal strImageSrcPath As String _
                              , ByVal strImageDesPath As String _
                              , Optional ByVal intWidth As Integer = 0 _
                              , Optional ByVal intHeight As Integer = 0) As String

        If System.IO.File.Exists(strImageSrcPath) = False Then Exit Function

        Dim objImage As System.Drawing.Image = System.Drawing.Image.FromFile(strImageSrcPath)

        If intWidth > objImage.Width Then intWidth = objImage.Width
        If intHeight > objImage.Height Then intHeight = objImage.Height
        If intWidth = 0 And intHeight = 0 Then
            intWidth = objImage.Width
            intHeight = objImage.Height
        ElseIf intHeight = 0 And intWidth <> 0 Then
            intHeight = Fix(objImage.Height * intWidth / objImage.Width)
        ElseIf intWidth = 0 And intHeight <> 0 Then
            intWidth = Fix(objImage.Width * intHeight / objImage.Height)
        End If

        Dim imgOutput As New Bitmap(objImage, intWidth, intHeight)
        Dim imgFormat = objImage.RawFormat

        objImage.Dispose()
        objImage = Nothing

        If strImageSrcPath = strImageDesPath Then System.IO.File.Delete(strImageSrcPath)
        ' send the resized image to the viewer
        imgOutput.Save(strImageDesPath, imgFormat)
        imgOutput.Dispose()

        Return strImageDesPath

    End Function


    Private Sub ButtonBrowseSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowseSource.Click
        With OpenFileDialog1
            .ShowDialog()
            strSource = .FileName
            TextBox1.Text = strSource
        End With

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With SaveFileDialog1
            .ShowDialog()
            strDestination = .FileName
            TextBox2.Text = strDestination
        End With
    End Sub

    Private Sub ButtonResize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonResize.Click
        If strSource <> vbNullString _
            And strDestination <> vbNullString _
                And TextBoxHeight.Text <> vbNullString _
                    And TextBoxWidth.Text <> vbNullString Then
            If (Me.ImageResize( _
                            strSource _
                           , strDestination _
                           , CInt(TextBoxWidth.Text) _
                           , CInt(TextBoxHeight.Text))) <> "" Then
                MsgBox("Resize completed.", MsgBoxStyle.Information)
            Else
                MsgBox("Error", MsgBoxStyle.Critical)
            End If

        End If
    End Sub

    Private Sub frmResize_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    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)



Comments and Discussions