RenderTargetBitmap 存在两个抗锯齿或子像素化问题:
1.
禁用位图本身的子像素化(例如,当它在 UIElement 中渲染时)。这是通过应用解决的:
RenderOptions.SetBitmapScalingMode(image1, BitmapScalingMode.NearestNeighbor);
(其中 image1 是 WPF Image 对象)。
仅支持 .NET 4 及更高版本。
在您的具体情况下,这并不重要,因为线条是逐像素渲染的。
2.
渲染到 RenderTargetBitmap 时禁用子像素化。可以通过RenderOptions.SetEdgeMode方法实现,参数值为EdgeMode.Aliased。
但是,此方法仅在以下情况下才有效:
因此,您可以按如下方式重写代码:
DrawingVisual visual = new DrawingVisual();
DrawingGroup group = new DrawingGroup();
Pen pen = new Pen(Brushes.Black, 1);
group.Children.Add(new GeometryDrawing(Brushes.YellowGreen, pen, new EllipseGeometry(new Point(0,0), 40, 40)));
for (int i = 0; i <= 100 - 1; i++)
group.Children.Add(new GeometryDrawing(null, new Pen(Brushes.Black, 1), new LineGeometry(new Point(0, i), new Point(i, i))));
RenderOptions.SetEdgeMode(group, EdgeMode.Aliased);
DrawingContext context = visual.RenderOpen();
context.DrawDrawing(group);
context.Close();
RenderTargetBitmap bmp = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32);
bmp.Render(visual);
image1.Source = bmp;