【发布时间】:2011-08-26 11:05:48
【问题描述】:
我正在通过在DrawingContext 上直接调用DrawLine 来绘制图表。由于我想避免任何抗锯齿功能,我尝试将 SnapToDevicePixels=true 放在父 UIElement 上,但我仍然有抗锯齿:
该项目是不是为 WPF4 编写的旧操作系统项目,但我将其重新定位到 Framework4,这也可能是一个问题吗?
【问题讨论】:
标签: wpf graphics antialiasing
我正在通过在DrawingContext 上直接调用DrawLine 来绘制图表。由于我想避免任何抗锯齿功能,我尝试将 SnapToDevicePixels=true 放在父 UIElement 上,但我仍然有抗锯齿:
该项目是不是为 WPF4 编写的旧操作系统项目,但我将其重新定位到 Framework4,这也可能是一个问题吗?
【问题讨论】:
标签: wpf graphics antialiasing
GuidelineSet 旨在解决您的问题。
@Marat Khasanov 推荐你使用GuidelineSet,你回答说它弄乱了你的代码。我也被这个问题困扰,所以我写了下面的代码,用不丑的代码来解决这个问题。
注意:此方法即使在Viewbox 中也有效。
public static class SnapDrawingExtensions
{
public static void DrawSnappedLinesBetweenPoints(this DrawingContext dc,
Pen pen, double lineThickness, params Point[] points)
{
var guidelineSet = new GuidelineSet();
foreach (var point in points)
{
guidelineSet.GuidelinesX.Add(point.X);
guidelineSet.GuidelinesY.Add(point.Y);
}
var half = lineThickness / 2;
points = points.Select(p => new Point(p.X + half, p.Y + half)).ToArray();
dc.PushGuidelineSet(guidelineSet);
for (var i = 0; i < points.Length - 1; i = i + 2)
{
dc.DrawLine(pen, points[i], points[i + 1]);
}
dc.Pop();
}
}
调用OnRender中的方法并传递线点给它。
protected override void OnRender(DrawingContext dc)
{
// Draw four horizontal lines and one vertical line.
// Notice that even the point X or Y is not an integer, the line is still snapped to device.
dc.DrawSnappedLinesBetweenPoints(_pen, LineThickness,
new Point(0, 0), new Point(320, 0),
new Point(0, 40), new Point(320, 40),
new Point(0, 80.5), new Point(320, 80.5),
new Point(0, 119.7777), new Point(320, 119.7777),
new Point(0, 0), new Point(0, 120));
}
【讨论】:
我找到了this link,他们基本上说你应该设置
ParentUIElement.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
它对我有用,所以值得一试!
【讨论】:
SnapsToDevicePixels 仅适用于元素边界框。您需要将 Guidelines 与 DrawingContext 一起使用。如果符合您的要求,您也可以指定VisualXSnappingGuidelines 和VisualYSnappingGuidelines。
【讨论】: