Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据wpf中的鼠标位置缩放图像_C#_Wpf_Image_Canvas_Zooming - Fatal编程技术网

C# 根据wpf中的鼠标位置缩放图像

C# 根据wpf中的鼠标位置缩放图像,c#,wpf,image,canvas,zooming,C#,Wpf,Image,Canvas,Zooming,我在放大画布上的图像时遇到了一些问题。我在考虑使用matrixtransform,但它不起作用。它返回了一个我不理解的异常!以下是XAML: <Grid> <Grid.RowDefinitions> <RowDefinition Height="40"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Button C

我在放大画布上的图像时遇到了一些问题。我在考虑使用matrixtransform,但它不起作用。它返回了一个我不理解的异常!以下是XAML:

<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>
如果有人能给我一个提示,那就太好了,谢谢

更换

transform.Matrix = matrix;


它是有效的;)

我不知道你是否解决了这个问题。但是这里的Xaml代码可以很好地进行缩放。它可以工作,你可以使用滚动摄像机的滚动条来平移图像

<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();
        }
    }

私有常数double _smallChange=0.1;
私有双_scaleFactor;
公共双尺度因子
{ 
得到
{ 
返回_scaleFactor;
} 
设置
{ 
_scaleFactor=值;
OnPropertyChanged(“ScaleFactor”);
} 
}
已加载私有无效窗口(对象发送器、路由目标)
{
双刻度=(grdItcMain.ActualWidth-20)/1920;
双刻度=(grdItcMain.ActualHeight-20)/1080;
双dScale=Math.Min(scaleX,scaleY);
ScaleFactor=dScale;//初始适合的大小
}
私有无效图像\u鼠标滚轮(对象发送器,鼠标滚轮事件参数e)
{
如果(e.Delta>0)
{
如果((ScaleFactor+\u smallChange)>25.0)
{
返回;
}
ScaleFactor+=\u smallChange;
vbxImageViewBox.LineRight();
vbxImageViewBox.LineRight();
vbxImageViewBox.LineRight();
vbxImageViewBox.LineRight();
vbxImageViewBox.LineRight();
vbxImageViewBox.LineDown();
vbxImageViewBox.LineDown();
vbxImageViewBox.LineDown();
vbxImageViewBox.LineDown();
vbxImageViewBox.LineDown();
vbxImageViewBox.LineDown();
}
其他的
{
如果((缩放因子-_smallChange)<0.001)
{
返回;
}
ScaleFactor-=\u小变化;
vbxImageViewBox.LineLeft();
vbxImageViewBox.LineLeft();
vbxImageViewBox.LineLeft();
vbxImageViewBox.LineLeft();
vbxImageViewBox.LineLeft();
vbxImageViewBox.LineUp();
vbxImageViewBox.LineUp();
vbxImageViewBox.LineUp();
vbxImageViewBox.LineUp();
vbxImageViewBox.LineUp();
vbxImageViewBox.LineUp();
}
}
这不是一个完美的解决方案,但它可以工作并且易于实现


道格

任何其他寻求解决方案的人都应该尝试一下威斯·沃夫·索尔特斯(WiesławŠoltés)创造的优秀解决方案。只需粘贴答案中显示的代码。

@SnowballTwo我仍然对scrollviewer有问题,大小或卷轴没有根据缩放调整大小:/n您应该去掉容器,将图像设置为scrollviewer的第一个子对象,初始设置与图像源匹配的图像宽度和高度,并使用LayoutTransform而不是RenderTransform。未测试…我找不到如何使用布局变换和后面的coe中的矩阵变换。如果你能给我一个暗示;)谢谢!只需将所有RenderTransforms替换为LayoutTransforms。。。它们都是“变换”类型。。。您可以按ctrl+shift+f并搜索项目中的所有用法
element.RenderTransform = new MatrixTransform( matrix );
<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();
        }
    }