2012-10-23 94 views
2

香港專業教育學院得到了panel和IM上panel .. 畫一個心臟,但我不想畫我想畫的一切,除了心臟因此心臟是透明的心臟。 我可以將從Path中選擇的Region反轉嗎?系統圖反轉地區打造出路徑

System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath(); 
      path.AddArc(0, 0, (this.Width/2), (this.Height/2), 135, 195); 
      path.AddArc((this.Width/2), 0, (this.Width/2), (this.Height/2), 210, 195); 
      path.AddLine((this.Width/2), this.Height, (this.Width/2), this.Height); 
      this.Region = new Region(path); 
      this.BackColor = Color.Black; 

它看起來像什麼(白=透明):1

我希望它看起來像(白色=透明)什麼: enter image description here

回答

4

我想你可以只添加2條圖形路徑在一起。

你可以試試這個代碼了:

private void panel1_Paint(object sender, PaintEventArgs e) 
{ 
    GraphicsPath path = new GraphicsPath(); 
    path.AddArc(0, 0, (this.Width/2), (this.Height/2), 135, 195); 
    path.AddArc((this.Width/2), 0, (this.Width/2), (this.Height/2), 210, 195); 
    path.AddLine((this.Width/2), this.Height, (this.Width/2), this.Height); 

    GraphicsPath path2 = new GraphicsPath(); 
    path2.AddRectangle(new Rectangle(new Point(0, 0), panel1.Size)); 

    path2.AddPath(path, false); 

    e.Graphics.FillPath(Brushes.Black, path2); 
} 

結果是:

Drawing the black heart on top of a background image

0

我不知道的方式反轉選區,但您始終可以在背景顏色中繪製一顆心,然後將背景顏色更改爲黑色。

this.BackColor = Form.BackColor; 
Form.BackColor = Color.Black; 
+0

這不會幫助我,因爲我想在另一個控制奠定它.. – Vloxxity

+0

我相信透明是有效的顏色選擇。但這對外面的黑人沒有幫助,所以這不會很有幫助。 – Bobson

2

你可以嘗試從您的面板的Paint事件的圖形對象中排除區域:

GraphicsPath path = new GraphicsPath(); 
path.AddArc(0, 0, (this.Width/2), (this.Height/2), 135, 195); 
path.AddArc((this.Width/2), 0, (this.Width/2), (this.Height/2), 210, 195); 
path.AddLine((this.Width/2), this.Height, (this.Width/2), this.Height); 

using (Region r = new Region(path)) { 
    e.Graphics.ExcludeClip(r); 
} 

// continue drawing... 
e.Graphics.Clear(Color.Yellow); 

,或者試圖修改控制的地區,那麼就使用該區域的排除屬性:

GraphicsPath path = new GraphicsPath(); 
path.AddArc(0, 0, (this.Width/2), (this.Height/2), 135, 195); 
path.AddArc((this.Width/2), 0, (this.Width/2), (this.Height/2), 210, 195); 
path.AddLine((this.Width/2), this.Height, (this.Width/2), this.Height); 

Region r = new Region(new Rectangle(Point.Empty, this.ClientSize)); 
r.Exclude(path); 

this.Region = r; 
0

原始解決方案here。我已經修改了您的需求

this.Region = InvertRegion(new Region(path), this.Width, this.Height); 

private Region InvertRegion(Region region, int width, int height) 
{ 
    Bitmap mask = new Bitmap(width, height); 
    Graphics.FromImage(mask).FillRegion(Brushes.Black, region); 

    int matchColor = Color.Black.ToArgb(); 

    Region inverted = new System.Drawing.Region(); 
    inverted.MakeEmpty(); 
    Rectangle rc = new Rectangle(0, 0, 0, 0); 
    bool inimage = false; 
    for (int y = 0; y < mask.Height; y++) 
    { 
     for (int x = 0; x < mask.Width; x++) 
     { 
      if (!inimage) 
      { 
       if (mask.GetPixel(x, y).ToArgb() != matchColor) 
       { 
        inimage = true; 
        rc.X = x; 
        rc.Y = y; 
        rc.Height = 1; 
       } 
      } 
      else 
      { 
       if (mask.GetPixel(x, y).ToArgb() == matchColor) 
       { 
        inimage = false; 
        rc.Width = x - rc.X; 
        inverted.Union(rc); 
       } 
      } 

     } 
     if (inimage) 
     { 
      inimage = false; 
      rc.Width = mask.Width - rc.X; 
      inverted.Union(rc); 
     } 
    } 
    return inverted; 
}