3

我目前正在尝试制作一个蒙版生成器来为图像生成多边形。这是我的代码。

Bitmap bmp = new Bitmap(file);
int w = bmp.Width;
int h = bmp.Height;
List<Point> vertices = new List<Point>();

for (int y=0; y<h; y++)
{
    bool rowbegin = false;
    for (int x=0; x<w; x++)
    {
        Color c = bmp.GetPixel(x, y);
        if (!rowbegin)
        {
            // Check for a non alpha color
            if (c.A != Color.Transparent.A)
            {
                rowbegin = true;
                // This is the first point in the row
                vertices.Add(new Point(x, y));
            }
        } else {
            // Check for an alpha color
            if (c.A == Color.Transparent.A)
            {
                // The previous pixel is the last point in the row
                vertices.Add(new Point(x-1, y));
                rowbegin = false;
            }
        }
    }
}

// Convert to an array of points
Point[] polygon = vertices.ToArray();

Graphics g = Graphics.FromImage(bmp);

g.DrawPolygon(Pens.LawnGreen, polygon);
g.Dispose();

但我得到错误的输出。

在此处输入图像描述

所需的。

在此处输入图像描述

我想要的也是字符之间的空白是空的。另外为什么 DrawPolygon 正在填充它?

谢谢。

4

1 回答 1

2

您是否尝试过这样的事情:我希望我的代码对您有所帮助:

在此处输入图像描述

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(@"C:\Users\Ali\Desktop\1.png");
            int w = bmp.Width;
            int h = bmp.Height;
            Lst_Data lastpointcolor = new Lst_Data() ;
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    Color c = bmp.GetPixel(x, y);
                    if (c.A != Color.Transparent.A)
                    {
                        if (lastpointcolor.color.A == Color.Transparent.A)
                        {
                            bmp.SetPixel(lastpointcolor.point.X, lastpointcolor.point.Y, Color.Red);
                        }
                    }
                    lastpointcolor = new Lst_Data() { point = new Point(x, y), color = bmp.GetPixel(x, y) };
                }
            }

            for (int y = h-1; y > 0; y--)
            {
                for (int x = w-1; x > 0; x--)
                {
                    Color c = bmp.GetPixel(x, y);
                    if (c.A != Color.Transparent.A)
                    {
                        if (lastpointcolor.color.A == Color.Transparent.A)
                        {
                            bmp.SetPixel(lastpointcolor.point.X, lastpointcolor.point.Y, Color.Red);
                        }
                    }
                    lastpointcolor = new Lst_Data() { point = new Point(x, y), color = bmp.GetPixel(x, y) };
                }
            }
            pictureBox1.Image = bmp;
        }
    }
    public struct Lst_Data
    {
        public Point point;
        public Color color;
    }
}

编辑:

还有我给你的另一个想法:D

为具有相同大小的原始图像制作蒙版并执行以下操作:

    Bitmap orginal = new Bitmap(@"C:\Users\Ali\Desktop\orginal.png");
    Bitmap mask = new Bitmap(@"C:\Users\Ali\Desktop\mask.png");
    for (int i = 0; i < orginal.Width; i++)
    {
        for (int j = 0; j < orginal.Height; j++)
        {
            if (orginal.GetPixel(i, j).A == Color.Transparent.A)
            {
                mask.SetPixel(i, j, Color.Transparent);
            }
        } 
    }
    Bitmap bitmap = new Bitmap(mask, mask.Height, mask.Height);
    using (Graphics g = Graphics.FromImage(bitmap))
    {
        g.DrawImage(mask, 0, 0);
        g.DrawImage(orginal,0,0);
    }
    pictureBox1.Image = bitmap;

结果:

在此处输入图像描述

在此处输入图像描述

于 2013-04-07T03:41:24.240 回答