Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am looking for a way to crop a rectangle from an image given information is the 4 corner point co-ordinates (x,y) in C# or vb.
The reason i don't want to follow usual approach that uses top left point co-ordinates and width and height because it gives me rectangle that is parallel to the axes (Rectangle could be a little tilted in my scenario). How do i write a function in C#/vb .net that has following syntax

Image img = Crop(bmp,x1,y1,x2,y2,x3,y3,x4,y4);

Where bmp is original image.
Posted
Updated 23-Jul-12 7:16am
v2
Comments
Kenneth Haugland 23-Jul-12 13:23pm    
If you work with WPF there is a spesific preprogramed code to do this for you... So Im guessing that this is WinForms

Please don't put 'urgent' in your question, we don't really care that you can't do your job and waited too late to look in to it.

As you say, a bitmap is always a rectangle. As such, if it's not possible to use random co-ordinates to do what you want, and not have extra info, even if you make it black or something. If the four points define a rectangle, but rotated, then you need to rotate the image to create a bitmap of it. As such, what you probably want to do, is work out how far you need to rotate the image. GDI+ has plenty of support for rotating the image, it's calculating how far to rotate and what the points of your rectangle will be, that will be the challenge.
 
Share this answer
 
Assuming that these four point define an arbitrary polygon, you will probably need to apply a mask.
You could proceed like this:
1) Create a mask image with the same size as your original (probably)
2) Draw your polygon as filled one onto this mask "layer"
3) Apply a masking algorithm using the mask and the original image (like here)
4) Define the bounding box of the polygon and crop the result

Alternatively, if suitable, you could move to RGBA space, and apply full transparency outside the mask.
This article[^], based on AForge[^] might also help you.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900