GDI +:如何填充三角形?

时间:2011-01-19 14:46:29

标签: gdi+ linear-gradients

我想填充矩形的左下半部分(即三角形):

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 个答案:

答案 0 :(得分: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)
    });