首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用LinearGradientBrush的FillPath

使用LinearGradientBrush的FillPath
EN

Stack Overflow用户
提问于 2013-04-24 18:55:33
回答 1查看 1.5K关注 0票数 2

我在这个主题GDI+ .NET: LinearGradientBrush wider than 202 pixels causes color wrap-around中发现了类似的问题,但解决方案对我不起作用。我也不能在这个话题中提问。请看一下。下面是我的代码:

代码语言:javascript
复制
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);
    }

}

我希望从左到右从暗到亮,但请看:

我试着画一个阴影,我坚持这样做。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-25 05:11:10

正如Hans在下面评论的那样,笔刷坐标需要与路径匹配,所以不是这样:

代码语言:javascript
复制
shadowBrush = new LinearGradientBrush(PointF.Empty,
    new PointF(this.ShadowThick + 1, 0), this.ShadowColor, Color.Transparent);

它应该是:

代码语言:javascript
复制
new LinearGradientBrush(new Point(this.width - this.ShadowThick, 0),
                        new Point(this.width, 0),
                        this.ShadowColor, Color.Transparent);

此外,您还应该处理您创建的图形对象。我还更改了你的画笔,只使用Point而不是PointF,因为你不需要处理浮点数。

以下是重写的代码:

代码语言:javascript
复制
  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);
    }
  }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16190150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档