代码之家  ›  专栏  ›  技术社区  ›  Idanis

将平移变换应用于图像C#

  •  1
  • Idanis  · 技术社区  · 5 年前

    我试图使用鼠标右键移动图像,使用 MatrixTransform (我正在使用 矩阵变换

    这是我的 Xaml

    <Image x:Name="StreamImage" Source="{Binding Image, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Stretch="None"
                                RenderTransform="{Binding Overlays.RenderTransform, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    <ItemsControl x:Name="StreamCanvasItemSource" ItemsSource="{Binding CanvasItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="{Binding ActualWidth, ElementName=StreamImage}" Height="{Binding ActualHeight, ElementName=StreamImage}">
             <ItemsControl.ItemsPanel>
                  <ItemsPanelTemplate>
                       <Canvas x:Name="StreamCanvas" Background="Transparent" Width="{Binding ActualWidth, ElementName=StreamImage}" 
                                Height="{Binding ActualHeight, ElementName=StreamImage}" 
                                Visibility="{Binding ImageExists, Converter={StaticResource BooleanToVisibilityConverter}}">
                           <Interactivity:Interaction.Triggers>
                                <Interactivity:EventTrigger EventName="PreviewMouseWheel">
                                    <Command:EventToCommand Command="{Binding Overlays.OnMouseWheelCommand}" PassEventArgsToCommand="True" />
                                </Interactivity:EventTrigger>
                                 <Interactivity:EventTrigger EventName="MouseMove">
                                     <Command:EventToCommand Command="{Binding Overlays.OnMouseMoveCommand}" PassEventArgsToCommand="True"
                                                                CommandParameter="{Binding ElementName=StreamImage}" />
                                 </Interactivity:EventTrigger>
                                    <Interactivity:EventTrigger EventName="MouseDown">
                                        <Command:EventToCommand Command="{Binding OnMouseDownCommand}" PassEventArgsToCommand="True" />
                                    </Interactivity:EventTrigger>
                                    <Interactivity:EventTrigger EventName="MouseEnter">
                                        <Command:EventToCommand Command="{Binding Overlays.OnEnterExitCommand}" CommandParameter="Enter" />
                                    </Interactivity:EventTrigger>
                                    <Interactivity:EventTrigger EventName="MouseLeave">
                                        <Command:EventToCommand Command="{Binding Overlays.OnEnterExitCommand}" CommandParameter="Leave" />
                                    </Interactivity:EventTrigger>
                          </Interactivity:Interaction.Triggers>
                   </Canvas>
               </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
    </ItemsControl>
    

    在我的 ViewModel :

    private MatrixTransform m_renderTransform = new MatrixTransform();
    private Point m_previouseLocation = new Point(-1, -1);
    
    public Point MouseLoaction { get; set; }
    public MatrixTransform RenderTransform
    {
        get => m_renderTransform;
        set
        {
            m_renderTransform = value;
            OnPropertyChanged();
        }
    }
    
    private void OnMouseMove(object parameter)
    {
        MouseLoaction = Mouse.GetPosition(image);
    
        if ((m_previouseLocation.X == -1) && (m_previouseLocation.Y == -1))
            m_previouseLocation = MouseLoaction;
    
        if (Mouse.RightButton == MouseButtonState.Pressed)
        {
            var matrix = RenderTransform.Matrix;
            matrix.TranslatePrepend(MouseLoaction.X - m_previouseLocation.X, MouseLoaction.Y - m_previouseLocation.Y);
            RenderTransform.Matrix = matrix;
        }
    
        m_previouseLocation = MouseLoaction;
    }
    

    }

    我也试过用 TransformGroup ,将 矩阵变换 TranslateTransform 作为移动的第二个孩子,但是当我做一个小动作的时候,图像是有边界的。

    我错过了什么?

    0 回复  |  直到 5 年前