How to crop a cross rectangle from an image using C #?

I want to get some parts of the image to crop the images. However, when I want to get a part that is not parallel to the image, I rotate the image and then crop it.

I do not want to rotate the image and crop the parallel rectangle. I want, without rotating the image, to crop the rectangle at an angle from the image.

Is there any way to do this?

I think I could not express myself well enough. This is what I want to do: a rough image .

Suppose the red thing is a rectangle :) I want to crop this thing from the image. After trimming it does not need an angel. Thus, mj may lie.

+4
source share
1 answer

This method should do what you requested.

public static Bitmap CropRotatedRect(Bitmap source, Rectangle rect, float angle, bool HighQuality) { Bitmap result = new Bitmap(rect.Width, rect.Height); using (Graphics g = Graphics.FromImage(result)) { g.InterpolationMode = HighQuality ? InterpolationMode.HighQualityBicubic : InterpolationMode.Default; using (Matrix mat = new Matrix()) { mat.Translate(-rect.Location.X, -rect.Location.Y); mat.RotateAt(angle, rect.Location); g.Transform = mat; g.DrawImage(source, new Point(0, 0)); } } return result; } 

(using the MJ example):

 Bitmap src = new Bitmap("C:\\mjexample.jpg"); Rectangle rect = new Rectangle(272, 5, 100, 350); Bitmap cropped = cropRotatedRect(src, rect, -42.5f, true); 
+5
source

All Articles