使用 ImageBrush 平铺时是否可以关闭抗锯齿?
Is it possible to turn off antialiasing when using ImageBrush tiling?
使用 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 的默认抗锯齿使它看起来很糟糕。我试过 UseLayoutRounding=true
、SnapsToDevicePixels=true
、RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality)
、
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.NearestNeighbor)
RenderOptions.SetEdgeMode(this, EdgeMode.Unspecified);
上 window。唯一改变任何视觉差异的是 BitmapScalingMode.NearestNeighbor
,但它看起来仍然很奇怪,而且平铺自身重叠。
编辑:下载完整的工作示例:WpfImageBrushExample.zip
这里的问题是 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));
产生:
使用 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 的默认抗锯齿使它看起来很糟糕。我试过 UseLayoutRounding=true
、SnapsToDevicePixels=true
、RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality)
、
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.NearestNeighbor)
RenderOptions.SetEdgeMode(this, EdgeMode.Unspecified);
上 window。唯一改变任何视觉差异的是 BitmapScalingMode.NearestNeighbor
,但它看起来仍然很奇怪,而且平铺自身重叠。
编辑:下载完整的工作示例:WpfImageBrushExample.zip
这里的问题是 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));
产生: