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

标签 c# winforms graphics gdi+ drawing2d

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

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

我不明白:

another buffer/temp graphics object

最佳答案

Barndon Moretz 解决方案的示例。

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

关于C# GDI - 如何创建多边形(点集)的位图副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6974458/

相关文章:

c# - 为什么它不将更改从 datagridview 保存到数据表中?

ios - 如何将 2 个 UIImageView 中的 2 个图像合并为 1 个图像

java - Java 中的几何变换序列

c# - 是否可以使用 .NET 跟踪文件操作?

c# - 获取类的第 n 个属性

c# - 在 Unity3D 中,从数字中删除除了一定数量的小数以外的所有内容

c# - 如何在 C# 中通过 DropBoxRest API 获取 Dropbox 文件?

c# - 确定来自另一个项目的窗体是否打开

c# - 在 WPF 表单上显示 HTML 片段

java - 图像和图形Java