在图像上绘制边框

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

Bitmap bmp = new Bitmap(file); int w = bmp.Width; int h = bmp.Height; List vertices = new List(); 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 (cA != 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 (cA == 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正在填充它?

谢谢。

你尝试这样的事情:我希望我的代码可以帮助你:

在此处输入图像描述

  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 (cA != 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 (cA != 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; 

结果:

在此处输入图像描述

在此处输入图像描述