c# – LinearGradientBrush无法正确呈现
发布时间:2020-12-15 07:45:31 所属栏目:百科 来源:网络整理
导读:请考虑标准System. Windows.Forms.Form中的以下代码 protected override void OnPaint(PaintEventArgs e){ base.OnPaint(e); Rectangle test = new Rectangle(50,50,100,100); using (LinearGradientBrush brush = new LinearGradientBrush(test,Color.Red,C
请考虑标准System.
Windows.Forms.Form中的以下代码
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Rectangle test = new Rectangle(50,50,100,100); using (LinearGradientBrush brush = new LinearGradientBrush(test,Color.Red,Color.Blue,0f)) { e.Graphics.DrawRectangle(new Pen(brush,8),test); } } 它产生了这个结果: 为什么红线和蓝线显示的顺序不正确,如何修复? 解决方法
渲染起源是问题所在.您要求的是宽度为8px的笔,并且8px被定义为从矩形定义的线的两个方向上向外4px.这是由于Alignment = Center的默认值.如果将Pen设置为使用Alignment = Inset,则会得到更好的结果.
只需将其添加到原始代码中即可看到此行: e.Graphics.DrawRectangle(Pens.White,test); 将您的方法更改为此,它将起作用: Rectangle test = new Rectangle(50,100); using (LinearGradientBrush brush = new LinearGradientBrush(test,0f)) { using (var pen = new Pen(brush,8f)) { pen.Alignment = PenAlignment.Inset; e.Graphics.DrawRectangle(pen,test); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |