C#GDI-如何创建多边形(点集)的位图副本 [英] C# GDI - How to Create a bitmap copy of a polygon (set of point)

查看:51
本文介绍了C#GDI-如何创建多边形(点集)的位图副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我听不懂:

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

推荐答案

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

这篇关于C#GDI-如何创建多边形(点集)的位图副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆