【问题标题】:C# GDI - How to Create a bitmap copy of a polygon (set of point)C# GDI - 如何创建多边形的位图副本(点集)
【发布时间】:2011-10-21 21:55:44
【问题描述】:

我有一个位图对象(甚至是任何其他图像),我正在这个位图上画一些线来创建一个多边形。 在绘图之后,我需要克隆/复制/剪切选择(基于线条)区域。

我不能使用 bitmap.clone 方法,因为它只适用于矩形。

我需要某种基于 Point[] 或 GraphicsPath 的克隆实现...

请帮助刚接触 GDI/图形... :)

更新

我试着做这样的事情:

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

你能提供一个代码示例吗?我是 GDI+ 的新手,无法实施您的建议。

我不明白:

另一个缓冲区/临时图形对象

【问题讨论】:

  • StackOverflow 不是论坛;如果您有新问题,请随时ask it。此外,如果您需要与海报互动,您可以在自己的问题中评论答案。

标签: c# winforms graphics gdi+ drawing2d


【解决方案1】:

您可以使用Graphics.Clip 指定从“源”位图/图像创建的自定义剪辑区域(来自GraphicsPath),然后在另一个缓冲区/临时图形对象上重绘它,这应该会给您想要的结果.

这不是最有效的解决方案,但它至少应该让你朝着正确的方向前进。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-25
      • 2014-11-23
      • 2015-07-01
      • 2019-08-10
      • 2019-10-03
      • 1970-01-01
      • 2014-05-13
      • 2012-06-17
      相关资源
      最近更新 更多