在wpf中获取图像的正确鼠标位置

时间:2017-12-13 07:27:22

标签: c# wpf canvas

我在这里做的是根据线分割图像。

这是我的应用程序的屏幕截图:

enter image description here

这是我的XAML代码

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Background="Gray">
    <Grid>
        <Image x:Name="img" Source="{Binding Imagesource, Source={x:Static vm:ListImagesVM.instance}}" />
        <ItemsControl ItemsSource="{Binding Path=ListLine, Source={x:Static vm:CropperVM.instance}}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas x:Name="canvas" Background="Transparent"  
                      Width="{Binding ElementName=img, Path=ActualWidth}"
                      Height="{Binding ElementName=img,Path=ActualHeight}" 
                      LayoutTransform="{Binding ElementName=img, Path=LayoutTransform}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseDown">
                                <ei:CallMethodAction MethodName="MouseDownEvent" 
                                  TargetObject="{Binding Source={x:Static vm:CropperVM.instance}}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>   
                    </Canvas>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Line X1="{Binding X1}" Y1="{Binding Y1}" X2="{Binding X2}" 
                      Y2="{Binding ElementName=img, Path=ActualWidth}" Stroke="Red" StrokeThickness="3"></Line>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</ScrollViewer>

这就是我获得职位的方式

public void MouseDownEvent(object s, MouseButtonEventArgs e)
{
    try
    {
        if (s == null) return;
        var canvas = s as Canvas;
        if (canvas == null) return;


        currentPoint = e.GetPosition(canvas);

        Console.WriteLine("Position: " + currentPoint.X);


    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        Console.WriteLine(ex.StackTrace);
        throw ex;
    }
}

该行的坐标为1175,但图片本身为7424 我将图像切成两半。我怎么解决这个问题?谢谢。

当我将线条放在图像的一半时,为什么线条的位置只有1175

0 个答案:

没有答案