2015-12-17 65 views
0

我有一些问题需要放大画布中的图像。我正在考虑使用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; 
} 

如果有人可以给我一个提示,这将是很好的,谢谢!

回答

1

更换

transform.Matrix = matrix; 

element.RenderTransform = new MatrixTransform(matrix); 

和它的作品;)

+0

yees!非常感谢你@SnowballTwo – Json

+0

@ SnowballTwo \t 我仍然有一个滚动查看器的问题,大小或滚动doesn,吨根据缩放调整:/ – Json

+0

你应该摆脱你的容器,设置图像为scrollviewer的第一个孩子,最初设置与图像源相匹配的图像宽度和高度,并使用LayoutTransform而不是RenderTransform。没有测试... – SnowballTwo

1

我不知道,如果你解决了这个呢。但是这里的Xaml代码可以让你很好地放大。它的工作原理和您使用ScrollVider的滚动条来平移图像。

<ScrollViewer x:Name="vbxImageViewBox" 
       CanContentScroll="False" 
       HorizontalScrollBarVisibility="Auto" 
       VerticalScrollBarVisibility="Auto"> 

    <Grid x:Name="grdItcMain" Margin="5"> 
     <Grid.LayoutTransform> 
      <TransformGroup> 
       <ScaleTransform 
        ScaleX="{Binding Path=ScaleFactor, 
          ElementName=this, 
          Mode=OneWay}" 
        ScaleY="{Binding Path=ScaleFactor, 
          ElementName=this, 
          Mode=OneWay}" /> 
      </TransformGroup> 
     </Grid.LayoutTransform> 
     <Rectangle Fill="DeepSkyBlue" Stretch="Fill" /> 
     <Image MouseWheel="Image_MouseWheel" 
      Source="{Binding PreviewSource, 
        ElementName=this, 
        Mode=OneWay}" 
      Stretch="Fill" /> 
    </Grid> 
</ScrollViewer> 

private const double _smallChange = 0.1; 
private double _scaleFactor; 
public double ScaleFactor 
{ 
    get 
    { 
     return _scaleFactor; 
    } 
    set 
    { 
     _scaleFactor = value; 
     OnPropertyChanged("ScaleFactor"); 
    } 
} 

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    double scaleX = (grdItcMain.ActualWidth - 20)/1920; 
    double scaleY = (grdItcMain.ActualHeight - 20)/1080; 
    double dScale = Math.Min(scaleX, scaleY); 
    ScaleFactor = dScale; //size to fit initially 
} 

private void Image_MouseWheel(object sender, MouseWheelEventArgs e) 
{ 
    if (e.Delta > 0) 
    { 
     if ((ScaleFactor + _smallChange) > 25.0) 
     { 
      return; 
     } 

     ScaleFactor += _smallChange; 

     vbxImageViewBox.LineRight(); 
     vbxImageViewBox.LineRight(); 
     vbxImageViewBox.LineRight(); 
     vbxImageViewBox.LineRight(); 
     vbxImageViewBox.LineRight(); 

     vbxImageViewBox.LineDown(); 
     vbxImageViewBox.LineDown(); 
     vbxImageViewBox.LineDown(); 
     vbxImageViewBox.LineDown(); 
     vbxImageViewBox.LineDown(); 
     vbxImageViewBox.LineDown(); 
    } 
    else 
    { 
     if ((ScaleFactor - _smallChange) < 0.001) 
     { 
      return; 
     } 
     ScaleFactor -= _smallChange; 

     vbxImageViewBox.LineLeft(); 
     vbxImageViewBox.LineLeft(); 
     vbxImageViewBox.LineLeft(); 
     vbxImageViewBox.LineLeft(); 
     vbxImageViewBox.LineLeft(); 

     vbxImageViewBox.LineUp(); 
     vbxImageViewBox.LineUp(); 
     vbxImageViewBox.LineUp(); 
     vbxImageViewBox.LineUp(); 
     vbxImageViewBox.LineUp(); 
     vbxImageViewBox.LineUp(); 
     } 
    } 

这不是一个完美的解决方案,但它的工作原理很容易实现。

Doug