1

I am trying to set a Image to a specific location on my application. I get the position that I want to set via Mouse.GetPosition. However, this position is not correct when I try to use Canvas.SetLeft. How can I set it to the position I want?

Note: This is a dynamically created Image Control onto a Canvas named SI, and XC[0] is the X coordinates that I got from Mouse.GetPosition.

var webimage = new BitmapImage(new Uri("C:\\Class16\\Images\\DSC_0000.jpg"));
            var imageControl = new Image();
            imageControl.Source = webimage;
            imageControl.Width =(XC[3] - XC[0]);
            imageControl.Height = (YC[1] - YC[0]);
            Canvas.SetLeft(imageControl, XC[0]);
            SI.Children.Add(imageControl);

And codes for getting the mouse position

private void Image_Frame_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (XC[3] == 0)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (DIP.Default.Image1BR == 0)
                {
                    Point position = e.GetPosition(this);
                    XC[variant] = position.X;
                    YC[variant] = position.Y;
                    variant++;
                }
            }
        }}

And if it helps, this is the Canvas Xaml portion.

<Canvas x:Name="SI" Height="380" Width="720" Canvas.Left="177" Canvas.Top="31">
        <Image x:Name="Image_Frame" Height="380" Width="720" Drop="Image_Frame_Drop" AllowDrop="True" MouseDown="Image_Frame_MouseDown" />
    </Canvas>
4

0 回答 0