0

I've got an image and I want to cut out a trapezoid from an image.

I've got 8 different coordinates like this (8 values to form a polygon with 4 Points)

pointA(222,222)
pointB(666,234)
pointC(678,235)
pointD(210,220)

I only found out how to crop images with bitmap.Clone like this

var x = pointA.x;
var y = pointA.y;
var height = pointD.y - pointA.y;
var width = pointB.x - pointA.x;
Bitmap image = new Bitmap(imagepath);
var rect = new Rectangle(x, y, wight, height);
var newImage = image.Clone(rect, image.PixelFormat);

This will create a straight rectangle and the important parts of the sub-parts wich I want to cut out are disappearing.

So, how to cut out a trapezoid using c# and .net framework core from a console environment?

I want to cut out a trapezoid but I just figured out the better form to describe what I want is a polygon.

derBjörn
  • 46
  • 8

1 Answers1

2

One way to do it is to use the Graphics API. This has a FillPolygon method that takes a list of points and a brush. To use the source bitmap you would use the TextureBrush. Put this together and you would end up with something like this:

    public Bitmap FillPolygon(Bitmap sourceBitmap, PointF[] polygonPoints)
    {
        var targetBitmap = new Bitmap(256, 256);
        using var g = Graphics.FromImage(targetBitmap);
        using var brush = new TextureBrush(sourceBitmap);
        g.FillPolygon(brush, polygonPoints);
        return targetBitmap;
    }

I think this should work from a windows console. There have historically been a few issues using some graphics APIs without a windows sessions, but I do not think this is a problem anymore.

For maximum compatibility you could always triangulate and rasterize the trapezoid yourself and copy the selected pixels, but it would probably be significantly more work.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • Thanks for your answer - After I figured out "trapezoid" is a polygon too, I edited my question and also tried to implement the answer from this - https://stackoverflow.com/questions/30954503/how-to-crop-a-polygonal-area-from-an-image-in-a-winform-picturebox but I get only black images – derBjörn May 17 '21 at 15:03
  • @derbjörn check that the target bitmap is large enough, and try drawing with for example `Brushes.White` to check that the drawing works. And some objects need to be disposed at the correct place for it to work correctly. – JonasH May 17 '21 at 15:09
  • the target bitmap is large enough, I tried some approaches with the solution. But I need to tinker a little bit more to archive my solution. Meanwhile (and thanks again for this hint with Graphis-API) I figured out how to draw on the image - instead of "brushing" the zone out, I am able to draw a polygon on the image, using the "Pen" - This works - But even with your hint, all "transparent" or "black" images (resolution is "correct" but nothing is in the picture) – derBjörn May 17 '21 at 15:26
  • _I get only black images_ Please show the code you are using now! - _8 different points_ What does that mean???? The 4(!) points form more like a triangle than a trapez... - Also: How large is the source bitmap? – TaW May 17 '21 at 18:36
  • I was able to create a solution, thanks to @JonasH <3 – derBjörn May 17 '21 at 23:06