VB.NET: How to Resize Image by Code

Some apps may need to upload photos or images. As developers, we usually limit the image size to reduce the server load. The easiest way is to limit the size and give a warning message if the image size is too large, but some users can also complain because they have to resize first.

So another way is resizing those image automatically before saving them into the database or file server. Can we di that with VB.NET code? Sure we can ^_^

I made an example with 3 option of resizing, so you can choose and modify it to fit your own project. 

  • Via Percentage
  • Via Width (image width) --> height automatically adjust
  • Via Height (image height)--> width automatically adjust

I didn't change the length and width at the same time to maintain the ratio of the image sizes.

UI:
Code:

Imports System.Drawing.Imaging

Public Class Form1

  Private Sub Form1_Load(ByVal sender As System.Object, _
                         
ByVal As System.EventArgs) Handles MyBase.Load

      PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

      PictureBox1.BorderStyle = BorderStyle.FixedSingle
      Label1.Text = "Image Source"
      Label2.Text = "Resize Type"
      Label3.Text = "Size to"
      Button1.Text = "Browse..."
      Button2.Text = "Resize"
      With ComboBox1
          .Items.Add("Percentage")
          .Items.Add("Width Scale")
          .Items.Add("Height Scale")
          .SelectedIndex = 0
      End With
      'default value of scale
      TextBox2.Text = 50

  End Sub

  Private Sub
 ComboBox1_SelectedIndexChanged(
ByVal sender As System.Object, _
          
ByVal As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

      If ComboBox1.SelectedIndex = 0 Then
          Label4.Text = "%"
      Else
          Label4.Text = "px"
      End If

  End Sub


  Public Sub ResizeImage(
ByVal ImageSource As String, _
                        
ByVal ResizeType As IntegerByVal scale As Single, _
                        
ByVal ResultPath As String)

      'source bitmap.
      Dim imgSource As New Bitmap(ImageSource)

      'resize type
      Select Case ResizeType
          Case 'percentage
              scale = scale / 100
          
Case 'width px
              scale = scale / (imgSource.Width)
          
Case 'height px
              scale = scale / (imgSource.Height)
      End Select

      'scale factor
      Dim sclFactor As Single = Single.Parse(scale)

      'bitmap for result
      Dim imgResult As New Bitmap( _
          CInt(imgSource.Width * sclFactor), _
          CInt(imgSource.Height * sclFactor))

      'Graphics object for result
      Dim grResult As Graphics = Graphics.FromImage(imgResult)

      'source to result bitmap
      grResult.DrawImage(imgSource, 0, 0, _
          imgResult.Width + 1, _
          imgResult.Height + 1)

      'save result
      imgResult.Save(ResultPath, ImageFormat.Jpeg)

  End Sub


  Private Sub
 Button1_Click(
ByVal sender As System.Object, _
              
ByVal As System.EventArgs) Handles Button1.Click

      With OpenFileDialog1
          .FileName = String.Empty
          .InitialDirectory = "C:\"
          .Title = "Open Image File"
          'filter image file
          .Filter = "Image Files |*.jpg;*.jpeg;*.png;*.bmp"
      End With
      Dim
 result 
As DialogResult = OpenFileDialog1.ShowDialog()
      If result = Windows.Forms.DialogResult.OK Then
          Try
              TextBox1.Text = OpenFileDialog1.FileName
              PictureBox1.ImageLocation = OpenFileDialog1.FileName
          Catch ex As Exception
              MsgBox("Error : " & ex.Message)
          End Try
      End If
  End Sub


  Private Sub Button2_Click(
ByVal sender As System.Object, _
              
ByVal As System.EventArgs) Handles Button2.Click

      SaveFileDialog1.Filter = "Image Files |*.jpg;*.jpeg;*.png;*.bmp"
      If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

          ResizeImage(TextBox1.Text, ComboBox1.SelectedIndex, _
                      TextBox2.Text, SaveFileDialog1.FileName)

          MsgBox("Image resized.")

      End If

  End Sub

End Class


Runtime: 
The image below shows how the application looks when it is first run. Click Browse... to choose image file.

 
Choose an image file.


Choose type of resize.
 
Click Resize button, and specify path and file name for saving.


 A message box will appear if the process is success.


Let's check on its result, compare the dimension before and after. 





Post a Comment

0 Comments