简体   繁体   English

C#GDI-如何创建多边形(点集)的位图副本

[英]C# GDI - How to Create a bitmap copy of a polygon (set of point)

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. 我不能使用bitmap.clone方法,因为它仅适用于矩形。

I need some kind of a clone implementation based on Point[] or GraphicsPath... 我需要某种基于Point []或GraphicsPath的克隆实现。

Please help new to GDI/Graphics... :) 请帮助GDI /图形新手... :)

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. 我是GDI +的新手,无法实现您的建议。

I dont understand the: 我不明白:

another buffer/temp graphics object 另一个缓冲区/临时图形对象

An example of Barndon Moretzs solution. Barndon Moretzs解决方案的一个示例。

        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);
        }

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. 您可以使用Graphics.Clip来指定从“源”位图/图像创建的自定义裁剪区域(来自GraphicsPath ),然后在另一个缓冲区/临时图形对象上重绘该片段,这将给您所需的结果。

This isn't the most efficient solution, but it should at least get you going in the right direction. 这不是最有效的解决方案,但它至少应该使您朝正确的方向前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM