1. 从Toolbox中拖动Image控件和Button按钮到设计器中
2. 设置Image控件的Stretch属性为Uniform
3 XAML代码:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ScrollViewer HorizontalScrollBarVisibility="Visible" Margin="30,30,30,250">
<Image Height="800" HorizontalAlignment="Left" Name="image1" Stretch="Uniform" VerticalAlignment="Top" Width="800" Source="/ImageControl;component/Images/Koala.jpg" />
</ScrollViewer>
<Button Content="Load" Height="72" HorizontalAlignment="Left" Margin="127,380,0,0" Name="btnLoad" VerticalAlignment="Top" Width="160" Click="btnLoad_Click" />
</Grid>
4. 加载图片事件:
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
//ImageControl;component/Images/Jellyfish.jpg
BitmapImage myImage = new BitmapImage(
new Uri("/ImageControl;component/Images/Jellyfish.jpg", UriKind.Relative));
image1.Source = myImage;
}
5.运行效果