2015-12-17 64 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