当我尝试在 WPF 中使用抗锯齿图形时,我遇到了一个奇怪的渲染问题。
这是一个简化版本。
如果我使用以下 XAML
<Window x:Class="RenderingBug.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Width="300" Height="300">
<Grid Name="myGrid" Background="AntiqueWhite" Width="250" Height="250">
<ScrollViewer Name="myScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Canvas Height="500" Width="500" Name="myCanvas" />
</ScrollViewer>
</Grid>
</Window>
以及以下cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RenderingBug
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
PathFigureCollection pfc = new PathFigureCollection();
PathFigure pf = new PathFigure();
pf.StartPoint = new Point(100, 20);
LineSegment ls = new LineSegment();
ls.Point = new Point(20, 100);
PathSegmentCollection psc = new PathSegmentCollection();
psc.Add(ls);
pf.Segments = psc;
pfc.Add(pf);
PathGeometry pg = new PathGeometry(pfc);
RectangleGeometry clippingRectangle = new RectangleGeometry(new Rect(0, 0, 80, 80));
Path p1 = new Path();
p1.ClipToBounds = true;
p1.Clip = clippingRectangle;
p1.StrokeDashCap = PenLineCap.Square;
p1.Stroke = Brushes.Black;
p1.StrokeThickness = 30;
p1.Data = pg;
myCanvas.Children.Add(p1);
Path p2 = new Path();
p2.ClipToBounds = true;
p2.Clip = clippingRectangle;
p2.StrokeDashCap = PenLineCap.Square;
p2.Stroke = Brushes.White;
p2.StrokeThickness = 10;
p2.Data = pg;
myCanvas.Children.Add(p2);
}
}
}
我在裁剪矩形边缘所在的抗锯齿上遇到了一个奇怪的渲染问题(运行程序,这将是相当明显的,但它是一条模糊的灰线,裁剪矩形截断了路径。)
我尝试了各种技术,例如将控件与特定像素对齐,并在各种控件上设置 SnapsToDevicePixels,希望这可以解决这个问题(删除额外的模糊灰色带),但似乎没有任何帮助。
有任何想法吗?