Click here to Skip to main content
11,500,106 members (61,563 online)
Click here to Skip to main content

Image Cropping with Image Resizing Using VB.NET

, 17 Jun 2008 CPOL 92 9.7K 39
Rate this:
Please Sign up or sign in to vote.
Simple Image Cropping with Image Resizing Using VB.NET

image_croppingVBNet/mainimg.jpg

Introduction

This article discusses simple image cropping with image resizing using VB.NET for developers.

Background

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:

  1. Load the image
  2. Crop the image
  3. Capture the image
  4. Resize the image (if you need)
  5. Save the image

Before we start creating, we need 2 picture box controls (PreviewPictureBox, crobPictureBox), 3 buttons (save, Cancel, 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.:

openDlg.ShowDialog()  
PreviewPictureBox.Image = System.Drawing.Bitmap.FromFile(openDlg.FileName)
crobPictureBox.Image = System.Drawing.Bitmap.FromFile(openDlg.FileName)    
'*********PreviewPictureBox and crobPictureBox are 
'the cropping and previewing pictureboxes  

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 have to create Colors(cropPenColor) 
'and Size(cropPenSize) to draw the doted lines  

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, _
	crobPictureBox.Width, crobPictureBox.Height)
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
'*************Image resizing is done by means of getting the values from trackbar  

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 SmoothingMode, CompositingQuality, InterpolationMode and 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.

History

  • 17th June, 2008: Initial post

License

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

Share

About the Author

Senthil Sambandam
Software Developer (Senior)
India India
No Biography provided
Follow on   LinkedIn

Comments and Discussions

 
Questionresize rectangel Pin
Member 1116588130-Oct-14 5:33
memberMember 1116588130-Oct-14 5:33 
QuestionAnother simple image cropping/resizing method Pin
Dina_Du20-Jun-13 18:19
memberDina_Du20-Jun-13 18:19 
QuestionThanks Pin
raveena_ff12-Jun-13 2:37
memberraveena_ff12-Jun-13 2:37 
QuestionNeed Help Additing,Selecting,Moving Shapes to picture box Pin
rexpertq21-May-13 2:54
memberrexpertq21-May-13 2:54 
GeneralMy vote of 5 Pin
yasir.koplak5-May-13 23:00
memberyasir.koplak5-May-13 23:00 
GeneralMy vote of 4 Pin
dedyfuadi31-Mar-13 20:37
memberdedyfuadi31-Mar-13 20:37 
GeneralMy vote of 5 Pin
John L Bird25-Sep-12 1:49
memberJohn L Bird25-Sep-12 1:49 
QuestionAcquiring Images from Scanners Image Cropping with Image Resizing Save Image to Specific folder Pin
Anbarasan Gopal28-Aug-12 10:37
memberAnbarasan Gopal28-Aug-12 10:37 
GeneralImage Cropping with Image Resizing Using VB.NET Pin
VovaZ21-Apr-12 18:25
memberVovaZ21-Apr-12 18:25 
GeneralMy vote of 5 Pin
Member 820778419-Mar-12 4:35
memberMember 820778419-Mar-12 4:35 
GeneralMy vote of 5 Pin
manoj kumar choubey20-Feb-12 22:06
membermanoj kumar choubey20-Feb-12 22:06 
QuestionDear Sir, Pin
ibrahimsediq8-Feb-12 19:38
memberibrahimsediq8-Feb-12 19:38 
SuggestionVery good Pin
Eugen-GM23-Jan-12 10:02
memberEugen-GM23-Jan-12 10:02 
QuestionMissing Return Statement Pin
jason272618-Jan-12 21:26
memberjason272618-Jan-12 21:26 
QuestionHow to open a image in photoshop using vb.net Pin
successprabhu4-Dec-11 17:34
membersuccessprabhu4-Dec-11 17:34 
AnswerRe: How to open a image in photoshop using vb.net Pin
Member 476416514-Jan-12 0:58
memberMember 476416514-Jan-12 0:58 
GeneralMy vote of 5 Pin
thilina_SAMPATH29-Aug-11 17:41
memberthilina_SAMPATH29-Aug-11 17:41 
GeneralMy vote of 5 Pin
aalhanane9-Jun-11 4:14
memberaalhanane9-Jun-11 4:14 
GeneralMy vote of 5 Pin
dim1317-Dec-10 10:40
memberdim1317-Dec-10 10:40 
GeneralMy vote of 5 Pin
ManojDancer23-Sep-10 2:44
memberManojDancer23-Sep-10 2:44 
GeneralMy vote of 4 Pin
Niranjan0017-Jul-10 0:08
memberNiranjan0017-Jul-10 0:08 
Questionproblem in cropping Pin
Member 400159626-Jun-10 0:11
memberMember 400159626-Jun-10 0:11 
GeneralReq for Poly shape Pin
naraimha27-Dec-09 22:50
membernaraimha27-Dec-09 22:50 
Questionhow to convert bmp image to hexa conversion ??? Pin
m2kannan29-Sep-09 21:07
memberm2kannan29-Sep-09 21:07 
GeneralMy vote of 2 Pin
Maxxi867-Sep-09 22:17
memberMaxxi867-Sep-09 22:17 
GeneralCropping Images Pin
nace2k216-Jun-09 21:37
membernace2k216-Jun-09 21:37 
GeneralRe: Cropping Images Pin
inzamamkhan5-Jan-11 19:37
memberinzamamkhan5-Jan-11 19:37 
GeneralImage Cropping with Image Resizing Using vb.net Pin
hdogan11-Dec-08 2:59
memberhdogan11-Dec-08 2:59 
GeneralRe: Image Cropping with Image Resizing Using vb.net Pin
Senthil S26-Dec-08 8:02
memberSenthil S26-Dec-08 8:02 
GeneralFix for crop from working from only top left Pin
Shane Stewart13-Sep-08 21:31
memberShane Stewart13-Sep-08 21:31 
Questionratio cropping Pin
TylerRowsell7-Sep-08 10:58
memberTylerRowsell7-Sep-08 10:58 
AnswerRe: ratio cropping Pin
niketrk20-Nov-08 17:55
memberniketrk20-Nov-08 17:55 
QuestionCrop only from left top to right bottom? Pin
Demaker2-Jul-08 1:10
memberDemaker2-Jul-08 1:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 17 Jun 2008
Article Copyright 2008 by Senthil Sambandam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid