Crop an image using VB.NET
In this article you will learn how to crop an image and create a new image with the cropped portion in VB.NET.
This is article demonstrates that how to crop an image. Cropping provides the selection of a portion from the image and a new image will be created from the cropped portion. The code snippets provides a drag enabled and resizable rectangle to select the portion of the image to be cropped.
First of all load the picture box using this on the form to show by means of bitmap display method.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim str As String = Application.StartupPath & "picture.gif"
PreviewPictureBox.Image = System.Drawing.Bitmap.FromFile(str)
crobPictureBox.Image = System.Drawing.Bitmap.FromFile(str)
End Sub
Now you can crop the image from the picture box using mouse move and mouse down events.
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim openDlg As New System.Windows.Forms.OpenFileDialog
openDlg.Filter = "All Files"
If openDlg.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Exit Sub
End If
If Not openDlg.FileName Is Nothing Then
PreviewPictureBox.Image = System.Drawing.Bitmap.FromFile(openDlg.FileName)
crobPictureBox.Image = System.Drawing.Bitmap.FromFile(openDlg.FileName)
End If
End Sub
Image Cropping