gpt4 book ai didi

wpf - 如何在 WPF 的边框上创建斜角?

转载 作者:行者123 更新时间:2023-12-04 06:29:24 24 4
gpt4 key购买 nike

我正在尝试在装饰器的子类中进行简单的绘图,类似于他们在这里所做的...

How can I draw a border with squared corners in wpf?

...除了单像素边框厚度而不是他们在那里使用的两个。但是,无论我做什么,WPF 都决定它需要进行“平滑”处理(例如,它不是渲染单像素线,而是渲染两条像素线,每条“一半”的不透明度约为 50%。)换句话说,它试图消除绘图的锯齿。我不想要抗锯齿绘图。我想说,如果我从 0,0 到 10,0 画一条线,我会得到一条单像素宽的线,它的长度正好是 10 像素,没有平滑。

现在我知道 WPF 会这样做,但我认为这就是他们引入 SnapsToDevicePixels 和 UseLayoutRounding 的具体原因,我在 XAML 中将这两个设置为“True”。我还确保我使用的数字是实际的整数而不是小数,但我仍然没有得到我希望的漂亮、清晰、一像素宽的线条。

帮助!!!

标记

最佳答案

看看这篇文章:Draw lines exactly on physical device pixels

UPD

链接中的一些有值(value)的报价:

The reason why the lines appear blurry, is that our points are center points of the lines not edges. With a pen width of 1 the edges are drawn excactly between two pixels.

A first approach is to round each point to an integer value (snap to a logical pixel) an give it an offset of half the pen width. This ensures, that the edges of the line align with logical pixels.

Fortunately the developers of the milcore (MIL stands for media integration layer, that's WPFs rendering engine) give us a way to guide the rendering engine to align a logical coordinate excatly on a physical device pixels. To achieve this, we need to create a GuidelineSet


protected override void OnRender(DrawingContext drawingContext)
{
Pen pen = new Pen(Brushes.Black, 1);
Rect rect = new Rect(20,20, 50, 60);

double halfPenWidth = pen.Thickness / 2;

// Create a guidelines set
GuidelineSet guidelines = new GuidelineSet();
guidelines.GuidelinesX.Add(rect.Left + halfPenWidth);
guidelines.GuidelinesX.Add(rect.Right + halfPenWidth);
guidelines.GuidelinesY.Add(rect.Top + halfPenWidth);
guidelines.GuidelinesY.Add(rect.Bottom + halfPenWidth);

drawingContext.PushGuidelineSet(guidelines);
drawingContext.DrawRectangle(null, pen, rect);
drawingContext.Pop();
}

关于wpf - 如何在 WPF 的边框上创建斜角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5626652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com