Home > Back-end >  how to crop image with polygon in c#
how to crop image with polygon in c#

Time:12-09

I'm making a labeling tool.

Goal :By drawing a polygon on the picture, you have to export the image ​​inside the polygon to the outside.

example ![Example extract enter image description here

This is what I drew in the my program.

my program polygon

But I don't know how to extract this region. I want to know how to extract this area. I have saved the vertices of the picture above in an object. But I don't know how to extract data from the image through these vertices

========================================

So I found this. enter image description here

original

enter image description here

If you use a small polygon, there is no output at all.

enter image description here

You will notice that the two images are slightly different.

It seems to me that the part where the center point is cut and extracted is different. I don't know if what I was thinking is correct.


CodePudding user response:

You would create a new bitmap, at least as large as the bounding box of your polygon. Create a graphics object from this new bitmap. You can then draw the polygon to this bitmap, using the original image as a texture brush. Note that you might need to apply transform matrix to translate from the full image coordinates to the cropped image coordinates.

Note that it looks like you have radiological images. These are typically 16 bit images, so they will need to be converted to 8bit mono, or 24bit RGB before they can be used. This should already be done in the drawing code if you have access to the source. Or you can do it yourself.

  • Related