2
votes

I have a bitmap object (or even any other image) and I'm drawing some lines on this bitmap to create a polygon. after the drawing I need to clone/copy/cut the selection (based on the lines) area.

I cant use the bitmap.clone method becuase its working only with rectangle.

I need some kind of a clone implementation based on Point[] or GraphicsPath...

Please help new to GDI/Graphics... :)

Update

I tried doing something like this:

Graphics g = pbImage.CreateGraphics();
g.Clip = new Region(path);
Image img = null;
g.DrawImage(img, new Point(0, 0));

Can you provide a code example? I'm new for the GDI+ and I cant implement what you suggested.

I dont understand the:

another buffer/temp graphics object

2
StackOverflow isn't a forum; if you have a new question, please feel free to ask it. Also, you can comment on answers within your own questions if you need to interact with the posters.user1228

2 Answers

4
votes

An example of Barndon Moretzs solution.

        int x = 0;
        int y = 0;
        int width = 0;
        int height = 0;

        Point[] pesource = null;
        GraphicsPath gpdest = new GraphicsPath();

        source = new Bitmap(Image.FromFile(@"IMAGEPATH"));

        //Your polygon
        pesource = new Point[]
        {
            new Point(10,100),
            new Point(30,150),
            new Point(40,170),
            new Point(60,120),
            new Point(70,250),
            new Point(40,300),
            new Point(10,250),
            new Point(30,150)
        };

        //Determine the destination size/position
        x = source.Width;
        y = source.Height;

        foreach (var p in pesource)
        {
            if (p.X < x)
                x = p.X;
            if (p.X > width)
                width = p.X;

            if (p.Y < y)
                y = p.Y;
            if (p.Y > height)
                height = p.Y;
        }

        height = height - y;
        width = width - x;



        gpdest.AddPolygon(pesource);
        Matrix m = new Matrix(1, 0, 0, 1, -x, -y);
        gpdest.Transform(m);

        //Create the Bitmap
        clipped = new Bitmap(width, height);

        //Draw on the Bitmap
        using (Graphics g = Graphics.FromImage(clipped))
        {
            GraphicsPath gpgdi = new GraphicsPath();
            g.SetClip(gpdest);
            g.DrawImage(source, -x, -y);
        }
2
votes

You can use the Graphics.Clip to specify a custom clipping region (from a GraphicsPath) created from your "source" bitmap/image, then redraw it on another buffer/temp graphics object which should give you the desired result.

This isn't the most efficient solution, but it should at least get you going in the right direction.