0

使用 ImageBrush 时是否可以在 WPF 中关闭抗锯齿功能?

给定以下代码:

var handleImage = new BitmapImage(new Uri($"pack://application:,,,/Resources/myimage.png"));
var imageBrush = new ImageBrush(handleImage);
imageBrush.AlignmentY = AlignmentY.Top;
imageBrush.AlignmentX = AlignmentX.Left;
imageBrush.Stretch = Stretch.Uniform;
imageBrush.Viewport = new Rect(0, 0, _handleImage.Width, _handleImage.Height);
imageBrush.ViewportUnits = BrushMappingMode.Absolute;
imageBrush.TileMode = TileMode.Tile;
drawingContext.DrawRectangle(imageBrush, null, new Rect(0, 0, width, height));

给了我类似的东西:WPF 输出

但我期待:预期产出

WPF 的默认抗锯齿使它看起来很糟糕。我在窗户上试过UseLayoutRounding=true, SnapsToDevicePixels=true, RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality), 。RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.NearestNeighbor) RenderOptions.SetEdgeMode(this, EdgeMode.Unspecified);唯一改变任何视觉差异的是BitmapScalingMode.NearestNeighbor它仍然看起来很奇怪并且平铺重叠自身。

编辑:下载完整的工作示例:WpfImageBrushExample.zip

4

1 回答 1

0

这里的问题是BitmapImage宽度和高度是不同的小数 - 在这种情况下,它5.333 x 6x666不是预期的4 x 5像素。如果我使用_handleImage.PixelWidth并且_handleImage.PixelHeight问题得到解决,并且在任何这些渲染选项集下都不会出现奇怪的别名。

var handleImage = new BitmapImage(new Uri($"pack://application:,,,/Resources/myimage.png"));
var imageBrush = new ImageBrush(handleImage);
imageBrush.AlignmentY = AlignmentY.Top;
imageBrush.AlignmentX = AlignmentX.Left;
imageBrush.Stretch = Stretch.Uniform;
imageBrush.Viewport = new Rect(0, 0, _handleImage.PixelWidth, _handleImage.PixelHeight);
imageBrush.ViewportUnits = BrushMappingMode.Absolute;
imageBrush.TileMode = TileMode.Tile;
drawingContext.DrawRectangle(imageBrush, null, new Rect(0, 0, width, height));

产生:预期的

于 2020-05-28T22:37:14.973 回答