Click here to Skip to main content
Licence CPOL
First Posted 17 Jun 2008
Views 77,974
Downloads 3,383
Bookmarked 29 times

Image Cropping with Image Resizing Using VB.NET

By | 17 Jun 2008 | Article
Simple Image Cropping with Image Resizing Using VB.NET
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)

About the Author

Senthil S

Software Developer

India India

Member

Hello,
This is Senthil.S. I am a Software Engineer at TCS. I am Currently Standing on .Net and Flex Platform.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralImage Cropping with Image Resizing Using VB.NET PinmemberVovaZ18:25 21 Apr '12  
GeneralMy vote of 5 PinmemberMember 82077844:35 19 Mar '12  
GeneralMy vote of 5 Pinmembermanoj kumar choubey22:06 20 Feb '12  
QuestionDear Sir, Pinmemberibrahimsediq19:38 8 Feb '12  
SuggestionVery good PinmemberEugen-GM10:02 23 Jan '12  
QuestionMissing Return Statement Pinmemberjason272621:26 18 Jan '12  
QuestionHow to open a image in photoshop using vb.net Pinmembersuccessprabhu17:34 4 Dec '11  
AnswerRe: How to open a image in photoshop using vb.net PinmemberMember 47641650:58 14 Jan '12  
GeneralMy vote of 5 Pinmemberthilina_SAMPATH17:41 29 Aug '11  
GeneralMy vote of 5 Pinmemberaalhanane4:14 9 Jun '11  
GeneralMy vote of 5 Pinmemberdim13110:40 7 Dec '10  
GeneralMy vote of 5 PinmemberManojDancer2:44 23 Sep '10  
GeneralMy vote of 4 PinmemberNiranjan0010:08 7 Jul '10  
Questionproblem in cropping PinmemberMember 40015960:11 26 Jun '10  
GeneralReq for Poly shape Pinmembernaraimha22:50 27 Dec '09  
Questionhow to convert bmp image to hexa conversion ??? Pinmemberm2kannan21:07 29 Sep '09  
GeneralMy vote of 2 PinmemberMaxxi8622:17 7 Sep '09  
GeneralCropping Images Pinmembernace2k221:37 16 Jun '09  
GeneralRe: Cropping Images Pinmemberinzamamkhan19:37 5 Jan '11  
GeneralImage Cropping with Image Resizing Using vb.net Pinmemberhdogan2:59 11 Dec '08  
Dear Sir,
Do you have the same code in c# for asp.net 2008? If you send me the c# code of the "Image Cropping with Image Resizing", I will be glad. Thank you very much.
 
Sincerely
HDOGAN
GeneralRe: Image Cropping with Image Resizing Using vb.net PinmemberSenthil S8:02 26 Dec '08  
GeneralFix for crop from working from only top left PinmemberShane Stewart21:31 13 Sep '08  
Questionratio cropping PinmemberTylerRowsell10:58 7 Sep '08  
AnswerRe: ratio cropping Pinmemberniketrk17:55 20 Nov '08  
QuestionCrop only from left top to right bottom? PinmemberDemaker1:10 2 Jul '08  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120604.1 | Last Updated 17 Jun 2008
Article Copyright 2008 by Senthil S
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid