WPF图像缩放 [英] WPF Image Zooming
问题描述
我有一个带有图像的Viewbox。这很好,因为Viewbox会缩放图像以适合窗口。但是,我需要能够将图像缩放到其完整大小并显示滚动条,我很难弄清楚如何执行此操作。
I have a Viewbox with an Image inside of it. This is great since the Viewbox will scale the Image to fit the window. However, I need to be able to zoom the image to its full size and show scroll bars and I am having a hard time figuring out how to do this.
这是什么我现在有。任何人都可以指出我如何修改它来实现上述功能吗?
Here's what I have right now. Can anyone give some pointers on how I can modify this to implement the above functionality?
<Viewbox x:Name="viewbox">
<StackPanel>
<Image x:Name="image" Source="ranch.jpg" />
</StackPanel>
</Viewbox>
编辑:
只是为了澄清。我需要两种方式来查看图像,适合窗口的视图框样式以及切换到显示滚动条但不调整图像大小的实际大小视图的能力。
Just to clarify. I need both ways of viewing the image, the viewbox style of fitting the window AND the ability to toggle to an Actual Size view that shows scrollbars and doesn't resize the image.
推荐答案
这里你不需要 Viewbox
,只需输入 Image
在 ScrollViewer
中并操纵 VerticalScrollBarVisibility
和 HorizontalScrollBarVisibility
属性,您可以使图像
缩放:
You don't need a Viewbox
here, by putting the Image
in a ScrollViewer
and manipulating the VerticalScrollBarVisibility
and HorizontalScrollBarVisibility
properties, you can make the Image
scale or not:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<CheckBox x:Name="chkActualSize" Grid.Row="0" Content="Actual Size"/>
<ScrollViewer Grid.Row="1">
<ScrollViewer.Style>
<Style TargetType="{x:Type ScrollViewer}">
<Setter Property="HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="VerticalScrollBarVisibility" Value="Disabled"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=chkActualSize}" Value="True">
<Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ScrollViewer.Style>
<Image Source="http://sipi.usc.edu/database/misc/4.1.01.tiff" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</ScrollViewer>
</Grid>
这篇关于WPF图像缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!