2011-01-19 143 views
1

我要填寫AA矩形的左下半部分(即,三角形):GDI +:如何填充三角形?

alt text

用的LinearGradient,從顏色要透明: alt text

灌裝半一個矩形:

alt text

我知道點(x,y)和該矩形的大小。

如果我嘗試使用LinearGradientBrush,執行我的線性漸變:

brush = new LinearGradientBrush(
     MakePoint(0, y), //bottom left corner 
     MakePoint(x, 0), //upper right corner 
     MakeColor(255, c), //fully opaque color 
     MakeColor(0, c)); //fully transparent color 
graphics.FillRectangle(brush, MakeRect(0, 0, w, h)); 

線性漸變畫筆填充整個矩形,如果它繼續填充矩形的其餘部分與最終這將被罰款(透明)顏色;而是它環繞:

alt text

我有我的LinearGradientBrush我怎麼樣,我只是想FillTriangleFillPolygon,而不是FillRectangle。除了沒有FillTriangle或FillPolygon,只有FillRectangle和FillEllipse。

link text

+1

您使用的是什麼版本的.NET?我明確地看到圖形對象內的一個FillPolygon方法。 http://msdn.microsoft.com/en-us/library/89sks199.aspx – 2011-01-19 14:53:07

+0

你知道什麼,那裏*是*一個。我發誓我只能找到`FillRectangle`和`FillEllipse`。 (http://msdn.microsoft.com/en-us/library/ms535958(v=VS.85).aspx)雖然,我沒有使用.NET,但這是GDI +。把這個改爲一個答案,我會接受它。 – 2011-01-19 15:22:23

回答

2

看到有一個FillPolygon在圖形庫。我認爲你應該可以這樣做:

brush = new LinearGradientBrush(
     MakePoint(x, y), 
     MakePoint(0, h), 
     MakeColor(255, c), //fully opaque color 
     MakeColor(0, c)); //fully transparent color 

graphics.FillPolygon(brush, new PointF[] { 
     new PointF(0, 0), 
     new PointF(0, h), 
     new PointF(w, h) 
    });