调整/缩放 WPF 图像时变形
我有一个显示图像的程序,用户可以调整窗口中图像的大小(例如通过滚动鼠标滚轮)。我使用 RenderTransform 缩放图像,如下所示:
<Image x:Name="CurrentImage">
<Image.RenderTransform>
<TransformGroup>
<TranslateTransform x:Name="Translate" X="0" Y="0"/>
<ScaleTransform x:Name="Scale" ScaleX="1" ScaleY="1"/>
</TransformGroup>
</Image.RenderTransform>
</Image>
然后我只需更改 ScaleX 和 ScaleY 属性。图像按预期调整大小。
然而,在某些比例下,图像会变得非常扭曲。低于约 20% 和高于约 80% 时,显示效果良好,但对于介于两者之间的分辨率,部分或全部图像会失真。请参阅下面的屏幕截图。
其他一些细节:
- 我尝试过的 2 台 Windows XP 计算机也出现了同样的损坏,但 Windows 7 笔记本电脑没有出现该问题。
- 我还尝试使用
Height
和Width
属性自行缩放图像来替换RenderTransform
,但这没有什么区别。 - 并非所有图像都有问题,而且有问题的图像有时会在不同的比例因子下出现问题。图像越大似乎效果越差,但我没有系统地测量过。
- .NET 3.5 和 .NET 4 RC 都表现出这种行为。
有没有人见过这种行为,你有什么想法吗?
原始大小的 20%:
替代文本 http://maylark.com/Scale20。 png
原始大小的 26%:
替代文本 http://maylark .com/Scale26.png
原始大小的 41%:
替代文本http://maylark.com/Scale41.png
原始大小的 64%:
替代文本 http://maylark.com/Scale64.png
原始大小的 80%:
I have a program that displays an image, and the user can resize the image in the window (e.g. by rolling the mouse wheel). I scale the image using a RenderTransform, like this:
<Image x:Name="CurrentImage">
<Image.RenderTransform>
<TransformGroup>
<TranslateTransform x:Name="Translate" X="0" Y="0"/>
<ScaleTransform x:Name="Scale" ScaleX="1" ScaleY="1"/>
</TransformGroup>
</Image.RenderTransform>
</Image>
Then I just change the ScaleX
and ScaleY
properties. The image resizes as expected.
However, at certain scales, the image gets very distorted. Below about 20% and above about 80% it displays fine, but for resolutions in between, some or all of the image is distorted. See screenshots below.
A couple other details:
- the 2 Windows XP machines I tried this on had the same corruption, but a Windows 7 laptop did NOT exhibit the problem.
- I also tried replacing the
RenderTransform
with just scaling the Image by itself using theHeight
andWidth
properties, but it made no difference. - Not all images have a problem, and the ones that do will sometimes do it at different scale factors. Larger images seem to be worse, but I have not systematically measured.
- .NET 3.5 and .NET 4 RC both exhibit the behavior.
Has anyone seen this behavior before, and do you have any thoughts?
At 20% of original size:
alt text http://maylark.com/Scale20.png
At 26% of original size:
alt text http://maylark.com/Scale26.png
At 41% of original size:
alt text http://maylark.com/Scale41.png
At 64% of original size:
alt text http://maylark.com/Scale64.png
At 80% of original size:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,经过几个小时的尝试隔离问题后,我决定在新的 Visual Studio 解决方案中重新开始,并将组件一一放回去。在新的解决方案中,一切都正常工作,最后我恢复了所有课程,一切仍然正常!
结果是构建设置:非功能版本针对“任何 CPU”进行多目标,但新解决方案针对“x86”。
显然,Windows XP x64 图形子系统中的“任何 CPU”程序存在一个故障,因为切换到 x86 或在 Vista / 7 上运行可以解决该问题。
我发布此内容是希望能够节省其他人的时间。
So after many hours of trying to isolate the problem, I decided to just start over in a new Visual Studio solution and one by one put the components back. Everything was working in the new solution, and finally I had all the classes back and everything was still working!
It turned out to be build setting: the non-functional version was being multi-targeted for "Any CPU," but the new solution was targeting "x86".
Apparently there is a glitch in the Windows XP x64 graphics subsystem for "Any CPU" programs, because either switching to x86 or running on Vista / 7 solved the problem.
I have posted this in the hopes that it will save someone else some time.
大量 WPF 渲染< /a> 错误是由视频驱动程序引起的,特别是 nVidia Quadro 卡。您是否尝试过更新驱动程序?
A lot of WPF rendering bugs are caused by video drivers, specifically with the nVidia Quadro cards. Have you tried updating your drivers?
尝试消除变量。操作系统、硬件和驱动程序(如第一个答案中指出)以及您正在使用的图像格式。您提到并非所有图像都有问题?这些图像格式有什么区别吗?例如。位深度、颜色等
您还可以在其他工具(例如 IrfanView)中调整图像大小,并产生相同的问题。这可能有助于指出驱动程序问题。
图像渲染的方式看起来很可疑,就像图像步幅被拧在了引擎盖下一样。
Try to eliminate the variables. OS, Hardware and drivers (as pointed out in the first answer) and also the image formats that you are using. You mentioned that not all images have the problem? Are there any differences with those image formats? eg. Bit depth, color etc.
Also can you resize the images in some other tool, say IrfanView, and create the same problems. That might help point to a driver issue.
The way the images are rendering looks suspiciously like the image stride is getting screwed under the hood.