Question

i want to fill the bottom-left half of a a rectangle (i.e. a triangle):

alt text

with a LinearGradient, going from color to transparent: alt text

Filling half a rectangle:

alt text

i know the point (x,y), and the size of the rectangle.

If i try using a LinearGradientBrush, to perform my linear gradient:

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));

The linear gradient brush fills the entire rectangle, which would be fine if it continued to fill the rest of the rectangle with the final (transparent) color; but instead it wraps around:

alt text

i have my LinearGradientBrush how i like, i just want to FillTriangle or FillPolygon, rather than FillRectangle. Except there is no FillTriangle or FillPolygon, only FillRectangle, and FillEllipse.

See also

link text

Was it helpful?

Solution

There is a FillPolygon in the Graphics library. I think you should be able to do it like this:

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)
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top