缩小图像以适合ScrollViewer尺寸 [英] Zoom Out Image to fit ScrollViewer Dimensions

查看:166
本文介绍了缩小图像以适合ScrollViewer尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,相应地缩放图像。我的XAML中有 Image ,附加了 ScrollViewer ,因此可以缩放。像这样:

I have a little issue with zooming accordingly an image. I have an Image in my XAML, with a ScrollViewer attached so it can be zoomed. Like this :

<ScrollViewer x:Name="ImageScrollViewer">
    <Image x:Name="ImagePanel" 
           Stretch="Uniform" 
           VerticalAlignment="Center" 
           HorizontalAlignment="Center"/>
</ScrollViewer>

我想做的是,如果图片太大而不适合图像控制并需要缩小(仅在这种情况下),我想以编程方式设置ScrollViewer的ZoomFactor,使其适合Image控件。我的问题是,我真的不知道如何确定这个因素,考虑到我的图像的宽度和高度。

What I'd like to do is that, in case the image is too big to fit the image control and needs to be zoomed out (only in this case), I'd like to programatically set the ZoomFactor of the ScrollViewer so it fits the Image control. My problem is, I don't really know how could I determine that factor, considering my image's width and height.

另外我不想缩放图片,以任何方式改变它,我在这个问题上看到了一些关于SO的类似主题,但到目前为止,没有一个适用于我的情况。我只需要缩小我的照片,直到没有可见的垂直/水平滚动条。

Also I'd like not to scale the picture, to change it in any way, I'd seen some similar topics on SO for this issue but so far, none have applied to my case. I just need to zoom out my picture until there's no vertical/horizontal scrollbars visible.

对于如何做到这一点的任何建议,非常感谢,谢谢!

Any suggestions on how to do this, are greatly appreciated, thank you!

推荐答案

您可以设置尺寸(宽度高度 Image )以匹配 ScrollViewer.ViewportWidth / Height ,因此默认情况下所有图像都可见。然后将 MinZoomFactor 设置为1,这样任何人都不会超出有意义的范围,而 MaxZoomFactor 会变成有意义的东西。

You can set the dimensions (Width, Height) of the Image to match the ScrollViewer.ViewportWidth/Height, so that by default all of the image is visible. Then set MinZoomFactor to 1 so that no-one zooms out beyond what makes sense and MaxZoomFactor to something that would make sense.

如果您想要更多控制权 - 请将图片大小属性保留为默认自动(以及 VerticalAlignment / Horizo​​ntalAlignment 不同于 Stretch ),比较<$的纵横比c $ c> ScrollViewer ( ViewportWidth / ViewportHeight )到图像 ActualWidth / ActualHeight 假设 Image.Stretch =无并且图像实际上已加载)。如果 ScrollViewer 宽高比更宽 - MinZoomFactor ,您将需要 ScrollViewer.ViewportHeight / Image.ActualHeight 。如果图像宽高比更宽 - MinZoomFactor ,您将需要 ScrollViewer.ViewportWidth / Image.ActualWidth MaxZoomFactor 应该为1,除非您想要缩放到每个图像像素的屏幕像素数以上(应用程序的缩放率为100%)。

If you want more control - leave the Image size properties on the default Auto (and VerticalAlignment/HorizontalAlignment different than Stretch), compare aspect ratios of the ScrollViewer (ViewportWidth/ViewportHeight) to the Image (ActualWidth/ActualHeight assuming Image.Stretch="None" and that the Image is actually loaded). If the ScrollViewer aspect ratio is wider - the MinZoomFactor you will want will be ScrollViewer.ViewportHeight / Image.ActualHeight. If the Image aspect ratio is wider - the MinZoomFactor you will want will be ScrollViewer.ViewportWidth / Image.ActualWidth. MaxZoomFactor should be 1 regardless unless you want to zoom to more than screen pixel per image pixel (at 100% scaling of the application).

您可能会发现我的博客文章很有趣:

You might find my blog post interesting:

http://blog.onedevjob.com/2012/07/21/creating-a-zoomable-scrollviewer-with -zoomsnappoints-in-winrt-xaml /

这篇关于缩小图像以适合ScrollViewer尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆