我尝试在我的窗口中显示图像:
<Window x:Class="Problem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<DockPanel>
<StackPanel Orientation="Vertical">
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<Image Source="cat.jpg" Stretch="Uniform">
<Image.LayoutTransform>
<RotateTransform Angle="90" />
</Image.LayoutTransform>
</Image>
</ScrollViewer>
</StackPanel>
</DockPanel>
</Window>
其中cat.jpg是1920x1080图像。
这里是结果:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9kbGFuaS5wbmcifQ==” alt =“在此处输入图像描述”>
如您所见,尽管我看不到完整的猫头,但VerticalScrollbar已禁用。此外,HorisontalScrollBar是不可见的。
我的问题是:如何启用滚动条以便在图像上滚动?
删除StackPanel
。它提供了无限的内容空间,因此ScrollViewer
具有图像的高度。如果需要在图像下堆叠一些东西,请在StackPanel
内创建一个ScrollViewer
:
<Window x:Class="Problem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<DockPanel>
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<StackPanel>
<Image Source="cat.jpg" Stretch="Uniform">
<Image.LayoutTransform>
<RotateTransform Angle="90" />
</Image.LayoutTransform>
</Image>
</StackPanel>
</ScrollViewer>
</DockPanel>
我也遇到了同样的问题,但是使用了一个自定义的Image类,该类只通过使用protected override void OnRender(DrawingContext dc)
函数中的DrawingContext来绘制图形。那时我不明白,我要么需要设置图像的大小(设置Width和Height属性),要么需要从drawingContext创建一个新图像并将其设为图像的源,以便调整实际图像的大小。
我从这里得到答案:Get images from DrawingGroup,解决问题的方法是每次使用图像的渲染功能时,都通过更改属性:
DrawingImage drawingImage = new DrawingImage(mBackingStore);
Width = drawingImage.Width;
Height = drawingImage.Height;