如何在 WPF 中的边框上创建斜角?
我正在尝试在装饰器的子类中进行简单的绘图,类似于他们在这里所做的...
...除了使用单像素边框厚度而不是他们在那里使用两个。然而,无论我做什么,WPF 都决定它需要进行“平滑”(例如,它不是渲染单像素线,而是渲染一条两像素线,每个“一半”大约为 50% 的不透明度。)换句话说,它试图对绘图进行抗锯齿处理。我不想要抗锯齿绘图。我想说的是,如果我从 0,0 到 10,0 画一条线,我会得到一条单像素宽的线,长度正好为 10 个像素,无需平滑。
现在我知道 WPF 可以做到这一点,但我认为这就是他们引入 SnapsToDevicePixels 和 UseLayoutRounding 的具体原因,我在 XAML 中将这两者设置为“True”。我还确保我使用的数字是实际整数而不是小数,但我仍然没有得到我所希望的漂亮、清晰、一像素宽的线条。
帮助!!!
标记
I'm trying to do simple drawing in a subclass of a decorator, similar to what they're doing here...
How can I draw a border with squared corners in wpf?
...except with a single-pixel border thickness instead of the two they're using there. However, no matter what I do, WPF decides it needs to do its 'smoothing' (e.g. instead of rendering a single-pixel line, it renders a two-pixel line with each 'half' about 50% of the opacity.) In other words, it's trying to anti-alias the drawing. I do not want anti-aliased drawing. I want to say if I draw a line from 0,0 to 10,0 that I get a single-pixel-wide line that's exactly 10 pixels long without smoothing.
Now I know WPF does that, but I thought that's specifically why they introduced SnapsToDevicePixels and UseLayoutRounding, both of which I've set to 'True' in the XAML. I'm also making sure that the numbers I'm using are actual integers and not fractional numbers, but still I'm not getting the nice, crisp, one-pixel-wide lines I'm hoping for.
Help!!!
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊啊啊……明白了! WPF 认为从 0,0 到 10,0 的线确实位于该逻辑线上,而不是像 GDI 中那样是像素行。为了更好地解释,请考虑 WPF 中的坐标代表在一张方格纸上绘制的线条,而像素是这些线条组成的正方形(假设为 96 DPI。如果它们不同,您需要进行相应调整.)
所以...为了让绘图引用像素位置,我们需要将绘图从线条本身移动到像素的中心(方格纸上的正方形),因此我们将所有绘图移动 0.5, 0.5 (再次假设 DPI 为 96)
因此,如果它是 96 DPI 设置,只需将其添加到 OnRender 方法中就可以了...
希望这对其他人有帮助!
中号
Aaaaah.... got it! WPF considers a line from 0,0 to 10,0 to literally be on that logical line, not the row of pixels as it is in GDI. To better explain, think of the coordinates in WPF being representative of the lines drawn on a piece of graph paper whereas the pixels are the squares those lines make up (assuming 96 DPI that is. You'd need to adjust accordingly if they are different.)
So... to get the drawing to refer to the pixel locations, we need to shift the drawing from the lines themselves to be the center of the pixels (squares on graph paper) so we shift all drawing by 0.5, 0.5 (again, assuming a DPI of 96)
So if it is a 96 DPI setting, simply adding this in the OnRender method worked like a charm...
Hope this helps others!
M
看一下这篇文章:精确地在物理设备像素上绘制线条
UPD
链接中的一些有价值的引言:
Have a look at this article: Draw lines exactly on physical device pixels
UPD
Some valuable quotes from the link: