我最近建立了一些用于显示图片的WPF应用程序。该应用程序使用画布来显示图像,而将该画布放置在ScrollView中。我需要使用滑块的值对图像进行缩小和放大。当图像的比例小于ScrollView的宽度时,一切正常。但是,当图像的比例接近或超过ScrollView的宽度时,WPF应用程序会卡住,并在输出控制台中显示错误,如"The thread 0x214c has exited with code 259 (0x103)"。
这是我放大图像的代码:
这是我放大图像的代码:
private void Zoom(double m)
{
if (this.img != null && this.img.Source != null)
{
double temp = defaultScale * m;
double ww = img.Source.Width * temp;
double hh = img.Source.Height * temp;
drawingCanvas.Width = ww;
drawingCanvas.Height = hh;
this.img.RenderTransform = new ScaleTransform(temp, temp);
}
}
而XAML代码的结构为
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="DragNDrop2.MainWindow"
Title="MainWindow" Height="448" Width="693">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="0*"/>
</Grid.RowDefinitions>
<Button Content="DRAW" HorizontalAlignment="Left" VerticalAlignment="Top" Width="95" RenderTransformOrigin="-0.76,0.455" Height="26" Click="DrawImage" Margin="98,0,0,0"/>
<Button Content="OPEN" HorizontalAlignment="Left" VerticalAlignment="Top" Width="93" Height="26" Click="OpenImage"/>
<StackPanel Margin="0,31,0,0">
<ScrollViewer x:Name="canvasViewer" HorizontalAlignment="Left" Height="377" VerticalAlignment="Top" Width="675" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible">
<Canvas x:Name="drawingCanvas" Height="258" VerticalAlignment="Top" Width="414" />
</ScrollViewer>
</StackPanel>
<Slider x:Name="scaleSlider" HorizontalAlignment="Left" Margin="211,4,0,0" VerticalAlignment="Top" Width="296" ValueChanged="scaleSlider_ValueChanged" Value="5.0" TickFrequency="0.01"/>
</Grid>
</Window>
到现在我仍然不明白为什么会发生这种情况。我尝试了所有的方法,但我仍然不理解出现错误的原因。
我上传了一些视频到YouTube上以便更直观地展示: https://youtu.be/CwLeYHMrW30
完整的源代码我发布在Google Drive上: https://drive.google.com/file/d/0B1irLqfPwjq0eVYyQnVvNC1qYUE/view?usp=sharing
如果有解决此问题的方案(或者解释),我将非常感激。