我有一些問題需要放大畫布中的圖像。我正在考慮使用matrixtransform,但它不起作用。它給我一個例外,我不明白!這裏是XAML:根據鼠標在wpf中的位置縮放圖像
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Click="Button_Click"/>
<ScrollViewer Grid.Row="2"
Name="scroll"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<Canvas Name="Container"
ClipToBounds="True"
MouseWheel="Container_MouseWheel"
>
<Image Name="ImageSource"
>
<Image.LayoutTransform>
<MatrixTransform/>
</Image.LayoutTransform>
</Image>
</Canvas>
</ScrollViewer>
</Grid>
後面的代碼:
private void Button_Click(object sender, RoutedEventArgs e)
{
Bitmap Bmp = new Bitmap(@"C:\Desktop\image1.bmp");
ImageSource.Source = CreateBitmapSourceFromGdiBitmap(Bmp);
Container.Width = Bmp.Width;
Container.Height = Bmp.Height;
}
private void Container_MouseWheel(object sender, MouseWheelEventArgs e)
{
var element = sender as UIElement;
var position = e.GetPosition(element);
var transform = element.RenderTransform as MatrixTransform;
var matrix = transform.Matrix;
var scale = e.Delta >= 0 ? 1.1 : (1.0/1.1); // choose appropriate scaling factor
matrix.ScaleAtPrepend(scale, scale, position.X, position.Y);
transform.Matrix = matrix;
}
如果有人可以給我一個提示,這將是很好的,謝謝!
yees!非常感謝你@SnowballTwo – Json
@ SnowballTwo \t 我仍然有一個滾動查看器的問題,大小或滾動doesn,噸根據縮放調整:/ – Json
你應該擺脫你的容器,設置圖像爲scrollviewer的第一個孩子,最初設置與圖像源相匹配的圖像寬度和高度,並使用LayoutTransform而不是RenderTransform。沒有測試... – SnowballTwo