如何在 Image(WPF) 中的图像上绘制一个点,带有事件的运行时

How to draw a point over an image in Image(WPF), runtime with event

我遇到以下问题:我在 WPF 中使用 Image 组件。我加载 Image 对我来说是 map。我想制作一个 Button,如果我点击,我将被允许点击我的 map 并画一个点。那个点应该是"unique",所以它应该记住coordinates/description(我会把它存入数据库)。 坐标只能从该图像中读取,而不是从整个表格中读取。 创建点后我需要 OnMouseClick 事件。

我 use/read 应该怎么做?

  1. Canvas 允许您使用 left/top 坐标放置对象。
  2. 您需要将 canvas 恰好放在与图像尺寸相同的图像上。
  3. 为此,您应该使用网格,因为它允许控件相互堆叠。
  4. 您可以使用 MouseLeftButtonDown 事件处理程序中的 e.GetPosition() 获取鼠标点击坐标。由于canvas遮盖了图片,所以你会根据图片得到坐标。

    <Grid>
        <Image x:Name="MapImg"  Source="img/map.gif" Stretch="Fill" MouseLeftButtonDown="Image_MouseLeftButtonDown_1"/>
        <Canvas x:Name="Cnv"/>
    </Grid>
    
     private void Image_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {  
            Ellipse ellipse = new Ellipse();
            ellipse.Fill = Brushes.Sienna;
            ellipse.Width = 100;
            ellipse.Height = 100 ;
            ellipse.StrokeThickness = 2;
    
            Cnv.Children.Add(ellipse);
    
            Canvas.SetLeft(ellipse, e.GetPosition(MapImg).X);
            Canvas.SetTop(ellipse, e.GetPosition(MapImg).Y);            
        }