2013-04-07 93 views
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(); 

但我得到错误的输出。

enter image description here

的需要。

enter image description here

我想什么也是人物之间的空隙是空的。另外为什么DrawPolygon正在填充它?

谢谢。

+0

是在一条精灵表这些图像的所有3。 – 2013-04-07 01:47:56

+0

是的。我只是想消除透明的空间。 – 2013-04-07 01:49:44

+0

你想要什么?你知道你可以用WPF中的1行XAML应用相同的效果吗?你为什么使用winforms?它不支持图形。 – 2013-04-07 02:54:01

回答

2

你想是这样的:我希望我的代码可以帮助您:

enter image description here

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; 

结果:

enter image description here

enter image description here

+0

@Sri Harsha Chilakapati:看我的编辑。 – KF2 2013-04-07 04:33:08