2013-04-07 4 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입니다. –

+0

예. 투명 공간을 없애고 싶습니다. –

+0

무엇을 원하십니까? WPF에서 한 줄의 XAML과 동일한 효과를 적용 할 수 있다는 것을 알고 계셨습니까? 왜 winforms를 사용하고 있습니까? 그것은 그래픽을 지원하지 않습니다. –

답변

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

관련 문제