如何修复 VisualBrush 丢失的线?

How to fix VisualBrush lost line?

在WPF中,我们可以用VisualBrush做一些类似ppt左边的东西

但是我看到当我将 VisualBrush 缩放到一个小 size.Like 图像时,VisualBrush 可能会丢失矩形中的线条:

您可以看到 VisualBrush 丢失了底线。

但是我想要的是如下图:

当我尝试使用使用RenderTargetBitmap 获取图像的BitmapImage 并使用线性插值算法缩放时会得到清晰的图像。

我可以更改VisualBrush的算法我认为它可能使用邻域像素算法。

有没有像VisualBrush一样性能好的printscreen算法

当我将搜索关键字更改为 ViewBox 时,我可以找到与此问题相同的问题:how to avoid a single pixel line disappear in wpf?

有一个名为 TransformedBitmap 的 class 可以使用默认缩放算法缩放您的 RenderTargetBitmap

使用下面的代码:

public static BitmapSource ToBitmapSource(this Visual visual, Size size)
{
    var bounds = VisualTreeHelper.GetDescendantBounds(visual);
    var width = (int) Math.Round(bounds.Width);
    var height = (int) Math.Round(bounds.Height);
    var bitmap = new RenderTargetBitmap(width, height, 96.0, 96.0, PixelFormats.Pbgra32);
    bitmap.Render(visual);
    return new TransformedBitmap(bitmap, new ScaleTransform(size.Width / width, size.Height / height));
}

我在我的演示中尝试了这个方法,得到了如下结果。您可能会注意到左上角的小矩形没有丢失任何东西。