-1

I have a simple canvas which has a code to obtain the location where mouse is clicked. is there a way, I can convert the co-ordinates into millimeters, namely X and Y axis?

thanks

 <Grid>
    <Canvas x:Name="canvasMain" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="AliceBlue" MouseLeftButtonDown="canvasMain_MouseLeftButtonDown"/>
</Grid>

private void canvasMain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var position = e.GetPosition(this.canvasMain);

        if(Keyboard.IsKeyDown(Key.LeftShift))
        {               
            MessageBox.Show("X->" + position.X + " - " + "Y->" + position.Y);
        }            
    }
Sandepku
  • 861
  • 14
  • 31

1 Answers1

0

Mouse position in Mouse down event shows a pixel : position.Y and position.X and you cannot convert pixel directly into mm or inch you have to get display resolution and check how many pixel equals to one mm and based on that you can calculate mm using pixel.

Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
  • 1
    This isn't actually "pixels" in WPF, but [Device-Independent Pixels](https://learn.microsoft.com/en-us/windows/win32/learnwin32/dpi-and-device-independent-pixels). In a DPI-aware WPF application there are typically 96 DIPs/inch. So you can typically convert directly. For an exact mapping your system must however have exact knowlegde of the screen size and resolution, which often is just a rough guess. – Clemens Sep 08 '21 at 12:54