IMPENTA
IMPENTA

Reputation: 389

FillPath with LinearGradientBrush

I found a similar problem from this topic GDI+ .NET: LinearGradientBrush wider than 202 pixels causes color wrap-around But the solution didn't work for me. I cannot ask in that topic as well. Please take a look. Here is my code:

public partial class Form1 : Form {

    int ShadowThick = 10;
    Color ShadowColor = Color.Gray;
    int width;
    int height;

    public Form1() {
        InitializeComponent();
        width = this.Width - 100;
        height = this.Height - 100;
    }

    private void Form1_Paint(object sender, PaintEventArgs e) {
        //e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        GraphicsPath shadowPath = new GraphicsPath();
        LinearGradientBrush shadowBrush = null;
        // draw vertical shadow
        shadowPath.AddArc(this.width - this.ShadowThick, this.ShadowThick, this.ShadowThick,
            this.ShadowThick, 180, 180);
        shadowPath.AddLine(this.width, this.ShadowThick * 2, this.width,
            this.height - this.ShadowThick);
        shadowPath.AddLine(this.width, this.height - this.ShadowThick,
            this.width - this.ShadowThick, this.height - this.ShadowThick);
        shadowPath.CloseFigure();
        // declare the brush
        shadowBrush = new LinearGradientBrush(PointF.Empty,
            new PointF(this.ShadowThick + 1, 0), this.ShadowColor, Color.Transparent);
        //shadowBrush = new LinearGradientBrush(PointF.Empty,
        //    new PointF(this.ShadowThick + 1, 0), this.ShadowColor, Color.Transparent);

        //e.Graphics.DrawPath(Pens.Black, shadowPath);

        e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
        e.Graphics.FillPath(shadowBrush, shadowPath);
    }

}

I want it dark to light from left to right, but see:

enter image description here

I am trying to draw a shadow and I stuck at this.

Upvotes: 2

Views: 1571

Answers (1)

LarsTech
LarsTech

Reputation: 81610

As Hans commented below, the brush coordinates need to match the path, so instead of this:

shadowBrush = new LinearGradientBrush(PointF.Empty,
    new PointF(this.ShadowThick + 1, 0), this.ShadowColor, Color.Transparent);

it should be:

new LinearGradientBrush(new Point(this.width - this.ShadowThick, 0),
                        new Point(this.width, 0),
                        this.ShadowColor, Color.Transparent);

Also, you should dispose of the graphic objects you create. I also changed your brush to just use Point instead of PointF since you aren't dealing with floating numbers.

Here is your code re-written:

  e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
  using (GraphicsPath shadowPath = new GraphicsPath()) {
    shadowPath.StartFigure();
    shadowPath.AddArc(this.width - this.ShadowThick, this.ShadowThick, 
        this.ShadowThick, this.ShadowThick, 180, 180);
    shadowPath.AddLine(this.width, this.ShadowThick * 2, this.width,
        this.height - this.ShadowThick);
    shadowPath.AddLine(this.width, this.height - this.ShadowThick,
        this.width - this.ShadowThick, this.height - this.ShadowThick);
    shadowPath.CloseFigure();
    using (var shadowBrush = new LinearGradientBrush(
        new Point(this.width - this.ShadowThick, 0),
        new Point(this.width, 0),
        this.ShadowColor, Color.Transparent)) {
      e.Graphics.FillPath(shadowBrush, shadowPath);
    }
  }

Upvotes: 2

Related Questions