title: WPF Pixels to desktop pixels
body: <p>I've been linked in the past to <a href="https://stackoverflow.com/questions/611427/wpf-canvas-height-in-pixels/611518#611518">this</a> response to a similar question on converting WPF pixel coordinates to desktop ones, however I'm not sure I understand the maths involved.</p>
  
  <p>Astonish' answer states that "Pixels per WPF Unit = ConstantWPFUnit size * monitor DPI;" and that "The constant WPF unit size is 1/96."</p>
  
  <p>In my case, I've taken the DPI from a graphics object which was created from the bitmap object (as I couldn't find the property the Astonish spoke of) that I created after taking a screenshot of the desktop, so I have:</p>
  
  <pre><code>Graphics g = Graphics.FromImage(bitmap);
  float WpfUnit = (1 / 96) * g.DpiX;
  </code></pre>
  
  <p>Given that the DPI being returned from the graphics object is 96, I am left with</p>
  
  <pre><code>WpfUnit = (1 / 96) * 96 = 1
  </code></pre>
  
  <p>However, WpfUnit is coming out as 0 for some unknown (to me) reason. The only way I can see to fix this is to say</p>
  
  <pre><code>if(WpfUnit == 0) WpfUnit = 1;
  </code></pre>
  
  <p>And even that doesn't really fix the issue, as the height value and top values, when multiplied by the WpfUnit as suggested in the linked answer, have nothing done to them aside from being multiplied by 1. </p>
  
  <p>So, in conclusion, I'm still stuck on converting WPF pixels to desktop pixels. Any help on this would be greatly appreciated.</p>