如何避免带有圆角的可缩放 UserControl 彩色边框的视觉伪影?
问题描述
我有一个 Form
,其中包含:
- 一个
TrackBar
(最小值=1,最大值=200,代表缩放百分比); - 一个
UserControl
与BorderStyle = BorderStyle.None
.
相关代码
表格1
来自设计器代码
来自代码隐藏
用户控件1
初始截图
使用轨迹栏后的屏幕截图
缩小后黄色边框右侧不可见,放大时右侧有多个黄色边框.
更新:
答案有效,但是控件的一部分超出了边界.curveSize = 20
右上角的屏幕截图:
对于 curveSize = 24
:
我建议使用稍微不同的方法来绘制边框和用户控件的内容,该方法还应该治愈控件被重绘.
当您为控件创建区域,然后按原样绘制区域时,绘制的外部边界不会消除锯齿:锯齿像素落在区域之外.当围绕区域的边界绘制边框时,当然会应用相同的效果.
在这里,我应用了一个
I have a Form
which contains:
- a
TrackBar
(minimum = 1, maximum = 200, represents zoom percent); - a
UserControl
withBorderStyle = BorderStyle.None
.
Relevant code
Form1
Fromdesignercode
Fromcode-behind
UserControl1
Initial screenshot
Screenshot after using the trackbar
The right side of the yellow border becomes invisible after zooming out, and when zooming in there are multiple yellow borders on the right side.
Update:
The answer Works, but there is a part of the control that goes beyond the border. Screenshot for top-right corner, for curveSize = 20
:
and for curveSize = 24
:
I suggest a slightly different method to draw the Border and the content of the User Control that should also cure the artifacts generated when the control is redrawn.
When you create a Region for a Control and then you paint the Region as it is, the outer borders of the painting are not anti-aliased: the aliased pixels fall outside the Region. The same effect of course is applied when a border is painted around the bounds of the Region.
Here, I apply a Scale Matrix and a Translate Matrix that scale and move the bounds of the Region on the inside of the outer Region that defines the control's bounds.
The size of the scale and the translate transformations are determined by the Pen size.
More information on the Matrix usage here: Flip the GraphicsPath
In this case, when the borders are painted, the outer, anti-aliased, section of the border is inside the Region bounds and the anti-aliasing is preserved.
The background color of the Control is set to Color.Transparent
(a User Control supports color transparency on its own).
I've also added a couple of (non decorated) properties that allow to define the inner Color (the Control's BackColor
) and Size and Color of the Border. The rest is more or less what it was before.
Sample results:
这篇关于如何避免带有圆角的可缩放 UserControl 彩色边框的视觉伪影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!