views:

593

answers:

2

If i wanted to crop an image in VB.net, how would I go about doing it? I am trying to let the user drag out the box they want (system.drawing.rectangle), and it will automatically remove the edges surrounding the box.

My first problem is primarily the fact that I cannot make the system.drawing.rectangle visible. It is not displaying at all, I am setting its location and height programmatically, but nothing is showing up. I know there is probably something fairly obvious I am missing...but I cannot seem to find it.

My larger issue, however, lies with the cropping itself. I cannot find any crop methods, at all. Is there a hidden one I am missing? Or must I code it myself? How would I go about doing this? It ought to be able to output to a bitmap image object.

Thanks for the help, I am surprised this hasn't been asked on here before....

A: 

There are a number of articles on these topics on CodeProject:

Pick your favorite flavor (though I encourage you to check out the C# projects - it shouldn't be too hard to convert).

VB
Image Cropping with Image Resizing Using vb.net

C#
Cropping Images An Easy to Use Image Resizing and Cropping Control
Image Processing using C# (see the Cropping section - I was able to use this code in one of my projects)

WPF/C#
WPF Interactive Image Cropping Control
A Photoshop-like Cropping Adorner for WPF

Jay Riggs
I tried the VB one, it failed to crop the image I selected.
Cyclone
Did you get an error? How did it 'fail'? I've linked a number of examples of code that does what you need to do - have you tried studying how these authors accomplished what you need to do and adapted their techniques to your own code?
Jay Riggs
It was not cropping from/to the correct locations, in fact, it was usually cropping rather randomly it seemed.
Cyclone
+1  A: 

Regarding your first problem: a Rectangle isn't by itself visible. You have to draw it on your canvas using the Graphics object's DrawRectangle(...) method. For drawing a selection tool, you'll want to set your Pen object's DashCap and DashPattern properties.

To "crop" an image, you basically want to take the portion of a larger image delineated by a smaller Rectangle, and turn it into a new Bitmap. This can be done using one of the 30 overloads of the Graphics object's DrawImage(...) method. You can either keep the cropped portion in its original dimensions (resulting in a smaller Bitmap than your original), or you can "blow it up" to something like the original image's size. If you do the latter approach, it is usually a good idea to set your Graphics object's InterpolationMode property to HighQualityBicubic (I think that's the one, anyway), since the default InterpolationMode is pretty crappy.

There are a number of different ways of rendering images in .Net - it might help if you posted some of your code, along with an explanation of the exact problems you're running into.

Here is another answer with a link to a sample app (and source code in C#, sorry) that may help you get started.

MusiGenesis
Okay, i solved cropping, now how do I make the rectangle drawing work in more than one direction? It only draws to the bottom right of the mouse, which makes no sense at all to me. I also had to calculate for window offset, and mouse offset (4,30), and now I am stuck on making this next part work.
Cyclone
@Cyclone: you might want to post some code, since I really don't know what kind of surface you're drawing on, or how you're displaying your Bitmap. I'm guessing that you're storing the point from your MouseDown event (e.Location), and then in each MouseMove event you're creating a new Rectangle using the e.Location from the MouseDown event and the difference between the e.Location from MouseMove and the original, and then drawing that Rectangle.
MusiGenesis
@Cyclone: you can't specify negative values for Width and Height in a Rectangle (actually you can, but nothing will be drawn), so you need to write a little logic code to deal with a movement up and to the left (in this case, you would instead use the e.Location from the MouseMove event for the location of the Rectangle, and then calculate the width by subtracting the e.Location[MouseDown] x and y values from e.Location[MouseMove].
MusiGenesis
The Point parameter in the constructor for Rectangle(Point location, Size size) always refers to the upper-left corner of the Rectangle.
MusiGenesis
Right, makes sense, ill work on that part later. I got the cropping to work using Clone, so that is good. See my newest question though, an outofmemory exception goes unhandled after the user crops the image 8 times or so.
Cyclone
Post some code, and vote up my damned answer. You think I'm doing this for my health? :)
MusiGenesis
Lol, you couldve just asked ;)
Cyclone