根据wpf中的鼠标位置放大图像

时间:2015-12-17 09:23:36

标签: c# wpf image canvas zoom

我有一些问题要放大画布中的图像。我正在考虑使用matrixtransform,但它不起作用。它给我一个例外,我不明白!这是XAML:

/usr/local/psa/admin/bin/httpdmng  --reconfigure-domains [my domain]
/usr/sbin/apachectl graceful

背后的代码:

<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>

如果有人能给我一个很好的提示,谢谢!

3 个答案:

答案 0 :(得分:1)

替换

transform.Matrix = matrix;

element.RenderTransform = new MatrixTransform( matrix );

它有效;)

答案 1 :(得分: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();
        }
    }

这不是一个完美的解决方案,但它有效且易于实施。

道格

答案 2 :(得分:0)

任何寻求解决方案的人都应该尝试由WiesławŠoltés创建的出色解决方案here。只需粘贴答案中显示的代码即可。