如何在 wpf 中创建不缩放的不透明蒙版?
好的,我创建了一个具有透明度的 PNG-24。它基本上是一个灰度图像,使用黑色和透明之间的“颜色”而不是黑色和白色。我这样做是为了可以将其用作彩色矩形的不透明蒙版,从而仅使用单个图形以我想要的任何颜色渲染图像。
然而,在我的一生中,我无法让 WPF 停止对 da*n 图像进行抗锯齿!
我已在应用画笔的矩形上设置“SnapesToDevicePixels”...我已将 ImageBrush 的比例设置为“无”...我已将其 ViewPort 和 ViewBox 设置为绝对单位并将它们的大小精确设置为源图像。但无论我如何尝试,WPF 仍然坚持尝试让事情顺利进行!这非常令人沮丧!
那么...有人知道如何使用图像作为不透明蒙版而不丢失我们所做的像素精确绘图吗?我只是希望 WPF 按照我们绘制的方式呈现该死的东西,期间!
Ok, I've created a PNG-24 with transparency. It's basically a grayscale image that uses 'colors' in between black and transparent instead of black and white. I did this so I can use this as the Opacity Mask of a colored rectangle, thus rendering the image in whatever color I want using only a single graphic.
However, for the life of me, I can't get WPF to stop anti-aliasing the da*n image!!
I've set 'SnapesToDevicePixels' on the rectangle to which the brush is applied... I've set the ImageBrush's Scale to 'None'... I've set its ViewPort and the ViewBox to absolute units and sized them exactly to the source image. But no matter what I try, WPF still insists on trying to smooth things out! This is VERY frustrating!!!
So... anyone know how to use an image as an opacity mask but not lose the pixel-precise drawing that we have done? I just want WPF to render the damn thing as we drew it, period!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已尝试重现您的问题。就像这样:
mask.png 包含一个简单的对角蒙版,就像矩形的一半是可见的,另一半是 100% 透明的。
recraangle 正在渲染像素完美(并且可以根据需要进行别名)。
我认为,您的 DPI 设置可能不是您的显示器本机的,并且 WPF 无法正确渲染图像。
I have tried to reproduce your problem. Simply like this:
mask.png contains a simple diagonal mask, like that half of rectange is visible and other half is 100% transparent.
And recrangle is rendering pixel perfect (and aliased, as you want).
I think, that you may a DPI setting, that is not native to your monitor, and WPF just can`t render images correctly.
知道了!这是一个布局问题,由于某种原因,没有简单的方法可以更改。但是,您可以设置一个名为 UseLayoutRounding 的值来修复此问题。我只是将它设置在根级别(对于这个仿制品,一个网格......)
......然后砰!效果就像一个魅力! “有点”像“SnapsToDevicePixels”,但用于元素的定位(即,它舍入所有与布局相关的值,如左、宽度等,而 SnapsToDevicePixels 在渲染时将布局对齐到屏幕上的像素。)
M
GOT IT! It's a layout issue that for some reason, there's no easy way to change. However, there's a value you can set called UseLayoutRounding that fixes it. I just set it at the root level (for this fauxample, a grid...)
...and BAM! Works like a charm! "Sort of" like a 'SnapsToDevicePixels' but for positioning of elements (i.e. it rounds all layout-related values like left, width, etc. whereas SnapsToDevicePixels snaps the layout to the on-screen pixels when rendering.)
M