C# WPF resolution independancy

cdpigisresolutionwpf

I am developing a map control in WPF with C#. I am using a canvas control e.g. 400 x 200 which is assigned a map area of e.g. 2,000m x 1,000m.

The scale of the map would be: canvas_size_in_meters / real_size_in_meters.

I want to find the canvas_size_in_meters.

The canvas.ActualWidth gives the Width in DIU's (Device Independant Units). So, 400 DIU's is 400/96 = 4,17 inches, PROVIDED that the physical resolution of my monitor is 96 dpi.

However, using a ruler, I found that the physical resolution of my monitor is 87 dpi. (There are only few monitors that ACTUALLY have 96 physical dpi)

That DPI difference (10%) translates to a +10% difference in the actual map control width on screen.

How do I measure the size of a WPF control in inches EXACTLY and regardless of screen resolution and DPI setting ?

Best Answer

How do I measure the size of a WPF control in inches EXACTLY and regardless of screen resolution and DPI setting ?

This isn't actually possible, because for it to work, WPF would have to know the resolution (in terms of DPI) of your monitor. Sounds nice in theory, but in practice windows doesn't know this information. This is why windows itself always assumes 96dpi blindly instead of being smarter about it.

Even if there were some way to manually tell it, or if your particular monitor has a custom driver that does pass the correct information to windows, this isn't going to work on anyone else's computer, so windows doesn't pass this information on to any applications.

The best you can do is draw a scale like google maps does. You know that 1 pixel == 1 mile, so you can draw a 50 pixel line on your map, with a label saying "this line equals 50 miles"

Related Topic