Michael
Michael

Reputation: 13614

How to crop sub-part of the image?

I need to crop sub-part from image.

For example,I have this image:

enter image description here

I need to crop the part of the image that in the red frame, I have four coordinates of the frame corners,

Any idea how to implement it?

Thank you in advance.

Upvotes: 0

Views: 373

Answers (1)

user2509901
user2509901

Reputation:

You can use Graphics.DrawImage();

    Rectangle cropRect = new Rectangle(...);
    Bitmap src = Image.FromFile(fileName) as Bitmap;
    Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);

    using(Graphics g = Graphics.FromImage(target))
    {
         g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel);
    } 

And if need, you can save target to a new file.

Also See : C# Tutorial - Image Editing: Saving, Cropping, and Resizing

Upvotes: 1

Related Questions