Applying a Translation transform to Image C#

655 Views Asked by At

I'm trying to use the right mouse button for moving an image, using a MatrixTransform (I'm using MatrixTransform since I'm also scaling the image).

Here's my Xaml code:

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

In my 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;
}

}

When I do the following, the image does move (translates) but it's very fuzzy, it's like the image is "bumping" while it moves.

I tried also using a TransformGroup, putting the MatrixTransform as the first child for scaling and TranslateTransform as the second child for moving, but then the image was way of bounds when I made a minor movement.

What am I missing?

0

There are 0 best solutions below