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

发布于 2024-11-28 14:32:58 字数 495 浏览 1 评论 0原文

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

我无法使用 bitmap.clone 方法,因为它仅适用于矩形。

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

请帮助 GDI/Graphics 新手...:)

更新

我尝试做这样的事情:

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

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

我不明白:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转瞬即逝 2024-12-05 14:32:58

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

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);
        }
思慕 2024-12-05 14:32:58

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文