我最近构建了一些用于显示图像的WPF应用程序。此应用程序使用画布显示图像。并且该画布放置在ScrollView中。我需要使用滑块中的值缩小和放大图像。当图像的比例小于ScrollView的宽度时,一切正常。但是当图像的比例接近ScrollView宽度或更多时,WPF应用程序就会卡住,并且在输出控制台中显示错误,如"线程0x214c已退出,代码为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驱动器上发布的完整源代码: https://drive.google.com/file/d/0B1irLqfPwjq0eVYyQnVvNC1qYUE/view?usp=sharing
如果能解决这个问题(或解释),我非常感激。
答案 0 :(得分:0)
也许你应该检查这个解决方案:
<Slider HorizontalAlignment="Left" Minimum="x" Maximum="y" x:Name="ZoomInput" ValueChanged="ZoomInput_OnValueChanged"/>
private void ZoomInput_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
ImageScaleTransform.ScaleX = e.NewValue;
ImageScaleTransform.ScaleY = e.NewValue;
CanvasScaleTransform.ScaleX = e.NewValue;
CanvasScaleTransform.ScaleY = e.NewValue;
SetImagePosiotionInBounds();
}
并为画布和图像添加ScaleTransform:
<Canvas x:Name="drawingCanvas" Height="258" VerticalAlignment="Top" Width="414">
<Canvas.RenderTransform>
<ScaleTransform x:Name="CanvasScaleTransform"></ScaleTransform>
</Canvas.RenderTransform>
</Canvas>
它对我有用。