我在这个主题GDI+ .NET: LinearGradientBrush wider than 202 pixels causes color wrap-around中发现了类似的问题 但解决方案对我不起作用。我也不能在这个话题中问。请看一下。这是我的代码:
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);
}
}
我希望从左到右照亮黑暗,但请看:
我想画一个阴影,我坚持这个。
答案 0 :(得分:2)
正如Hans在下面评论的那样,画笔坐标需要与路径匹配,所以不要这样:
shadowBrush = new LinearGradientBrush(PointF.Empty,
new PointF(this.ShadowThick + 1, 0), this.ShadowColor, Color.Transparent);
它应该是:
new LinearGradientBrush(new Point(this.width - this.ShadowThick, 0),
new Point(this.width, 0),
this.ShadowColor, Color.Transparent);
此外,您应该处理您创建的图形对象。我也改变你的画笔只使用Point而不是PointF,因为你没有处理浮动数字。
这是你的代码重写:
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);
}
}