This article discusses simple image cropping with image resizing using VB.NET for developers.
I wondered once upon a time about image cropping. I learned and understand that there are many simple image cropping techniques available compared to other cropping technology currently available.
Using the Code
This article discusses simple image cropping using VB.NET.
In this article, I discuss simple image cropping by using the common events like mouse move, mouse down, mouse up and button Click events.
I would like to discuss image cropping using five steps only. You need to follow these steps only.
The steps are as listed below:
- Load the image
- Crop the image
- Capture the image
- Resize the image (if you need)
- Save the image
Before we start creating, we need 2 picture box controls (
crobPictureBox), 3 buttons (
open), 1 trackbar (
resizingTrackBar) and some labels. Hey, this seems like cooking specifications.
The steps are briefly described below:
1. Load the Image
The image can load to the picture box using file open dialog box and be made to show by means of bitmap display method, i.e.:
PreviewPictureBox.Image = System.Drawing.Bitmap.FromFile(openDlg.FileName)
crobPictureBox.Image = System.Drawing.Bitmap.FromFile(openDlg.FileName)
This can done either in form load or Button click events.
2. Crop the Image
The image can crop from the picture box using mouse move and mouse down events. This is possible by getting the x and y axis using the above events, i.e.:
cropX = e.X
cropY = e.Y
cropPen = New Pen(cropPenColor, cropPenSize)
cropPen.DashStyle = DashStyle.DashDotDot
You can see the styles that I applied to specify the selected area by
DashStyle.DashDotDot. And also by
crobPictureBox.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight).
3. Capture the Image
The image is captured only by using mouse up event, and the cropped image is set to
Bitmap class which makes a image and sets the created image for preview, i.e.:
Dim bit As Bitmap = New Bitmap(crobPictureBox.Image, _
g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
PreviewPictureBox.Image = cropBitmap
4. Resize the Image
If you want to resize the image, you can use the trackbar to resize the image. I prescribe use of a high quality image for resizing which results in quality cropped and resized images.
scale_factor = Integer.Parse(resizingTrackBar.Value)
img1.Image = cropBitmap
bm_source = New
Bitmap(img1.Image) bm_dest = New Bitmap( _
CInt(bm_source.Width * scale_factor), _
Int(bm_source.Height * scale_factor))
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + i, bm_dest.Height + i)
PreviewPictureBox.Image = bm_dest
5. Save the Image
The cropped image may be a low quality one which leads to image distortion. I tried my level best to make the image with good quality. I used
EncoderParameter(myEncoder, 60L) properties to make the images.
Points of Interest
In the above steps and coding methodology, you find simplicity. This is what I meant to do. All coders should use simple technique in an optimized manner for complicated issues.
- 17th June, 2008: Initial post