I have the following problem: I'm using Image
component in WPF. I load my Image
that is a map
for me. I want to make a Button
that if I click on, I will be allowed to click on my map
and draw a point. That point should be "unique", so it should remember coordinates/description(I will store it in a database).
Coordinates should be read only from that image, not from the whole form.
I need OnMouseClick
event after my point creation.
What should I use/read about to do that ?
You can get the mouse click coordinates using e.GetPosition()
in MouseLeftButtonDown
event handler. As canvas is covering up image, so you will get coord according to image.
<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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With