2015-12-11 67 views
0

我想畫一個矩形的顏色(在我的情況下是黑色)彎曲的區域。如何用C#Winform中的顏色填充曲面區域?

我試過許多東西FillPie,FillEllipse函數在OnPaint事件,但沒能做到,我要畫這樣Fill Curved Area

我曾嘗試下面的代碼。但這不是我想要的

protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     Graphics gr = e.Graphics; 
     int x = 50; 
     int y = 50; 
     int width = 100; 
     int height = 100; 
    Rectangle rect = new Rectangle(x, y, width/2, height/2); 
    gr.FillRectangle(Brushes.Black, rect); 
    gr.FillPie(Brushes.White, x, y, width, height, 180, 90); 

    using (Pen pen = new Pen(Color.Yellow, 1)) 
     gr.DrawArc(pen, x, y, width, height, 180, 90); 
} 

這段代碼是這樣畫的。我不想創建額外的矩形。 MyCode

回答

0

我不是100%肯定,如果這是你想要的東西:

enter image description here

我用指定的矩形的四分之一創建Region實現這一點。然後我用GraphicsPath排除它的派。然後使用帶有黑色刷子的Graphics.FillRegion填充得到的曲線:

protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     Graphics gr = e.Graphics; 
     int x = 50; 
     int y = 50; 
     int width = 100; 
     int height = 100; 

     Rectangle rect = new Rectangle(x, y, width/ 2, height/2); 
     Region r = new Region(rect); 

     GraphicsPath path = new GraphicsPath(); 
     path.AddPie(x, y, width, height, 180, 90); 
     r.Exclude(path); 
     gr.FillRegion(Brushes.Black,r); 
    }