Finding DPI in silverlight application - silverlight

I am working on silverlight application and finding the DPI of Canvas.
so how I can find it.
Thanks.

All measurements in Silverlight are in logical pixels where there are 96 DPI, regardless of the actual DPI of the device being rendered on.

Related

WPF Application doesn't fit into all the monitor screen

I have a WPF application that fits into my computer screen having screen resolution 1920 * 1080. But the same WPF application doesn't fit into my Microsoft Surface screen having same resolution 1920 * 1080. The exact issue is, it doesn't show the scroll bar in that little laptop. The issue is in the XAML for the page I am having trouble with and the window that hosts it. Any suggestion would be appreciated. Thanks.
The code is something like this right now.
<Page x:Class="form_Dock" Background="#FF19191E" MouseEnter="Page_MouseEnter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ABC.UI" SnapsToDevicePixels="True">
This could be a scaling issue caused by WPF using device independent pixels to determine the size of the application. The Surface screen is pretty small, so there may be some scaling done in Windows to compensate, which is throwing the size of your window out.
You might be able to fix your problem by setting SnapsToDevicePixels on the window element to true:
https://msdn.microsoft.com/en-us/library/system.windows.uielement.snapstodevicepixels(v=vs.110).aspx
Also, this explains device independent pixels quite well:
https://wpf.2000things.com/2013/02/19/759-device-independent-units-dips/

My WPF app is per-monitor dpi-aware out of the box. I was not expecting that?

I am a bit confused about per-monitor dpi-aware in WPF. I thought you need to do some work to make your windows scale properly on different monitors (as described in Developing a Per-Monitor DPI-Aware WPF Application).
But I've just ran my app on pc with two monitors (2560x1440 and 2160x1440) and the dialogue would automatically scale itself when I move it between monitors. That's on the latest fast ring Windows 10. Am I missing something?
What you see is an example of System scaling when one app window moves to a different monitor with a different DPI. That is because WPF apps are by-default System DPI Aware. As a result, if you notice carefully, you'll see WPF visuals/text gets blurred when the target DPI is higher or they look fuzzy when the target DPI is lower.
Also, note that monitor resolution does not matter for WPF apps, since WPF is device resolution agnostic (it's measurement unit is Device independent Pixels).
Good news : .NET 4.6.2 preview just got released and it hasPer Monitor DPI Awareness out of the box. Check out the developer guide and samples here :
https://github.com/Microsoft/WPF-Samples/tree/master/PerMonitorDPI
Continuing the conversation from comments.
Yes that is the same for Windows 8.1.
And here is the note from your linked post
Windows Presentation Foundation (WPF) applications are by default system DPI-aware.
HTH

Microsoft Surface & WPF

I have WPF application work with 1024x768 screen resolution, now I want to run this application on Microsoft Surface with wide resolution, I had make new windows with resolution 1366x768, but unfortunately I can't make it work fine on Surface, I tested it on my laptop, it's work 100%, but I don't understand why it would't work in the same way on Surface.
If there is anyone that has experience with Surface can you please help?
Update
When I run the WPF windows with resolution 1920x1080 work full screen on my laptop, but the problem is when run application on Surface with the same resolution don't fit the full screen, see this screenshot to more clarity:
Maybe your surface and laptop have different DPI settings. See here how to handle that problem.
From the information provided it may be that your WPF window layout doesn't sufficiently take into account differences in Display text and item sizing on different devices.
On both your Surface Pro and your laptop:
Select "Screen Resolution" from the Desktop context menu.
Select "Make text and other items larger or smaller" from the Screen
Resolution window.
Check the position of the "Change the size of all items" slider.
By default the Surface Pro is set to Larger. A new Windows 8 install will be set to Smaller.
In WPF you should not be setting a constant window size, instead you should:
Make the window's content react to the window size by using auto sizes and grids instead of constant sizes and panels
You can use WindowState="Maximized" to make the window full screen
You can prevent your window from being reduced too much with minwidth and minheight.

When using the WinForms designer, do I always need to have DPI set to 96?

With my current monitor I prefer a DPI setting of 120 pixels per inch (which windows suggests as the default). However, after designing a form, it often lays out incorrectly on systems that don't use 120 pixels per inch.
I'm wondering, is it necessary that I should set my display settings to 96 pixels per inch for whenever I use the designer?
Also, there are some problems when other developers have different DPIs. They open a form in the designer and move something like a text edit control, and suddenly find that it automatically resizes itself too. Then, there's one control that's a different size to the others and we're in a mess.
P.S. I've read related posts. They're all interesting, but didn't answer my question.
How to control the font DPI in .NET WinForms app
C# WinForms disable DPI scaling
WinForms Different DPI Layouts
DPI not scaling properly
Visual Studio and DPI issue
No. You don't need to always have the DPI set to 96 when using the WinForms designer.
If you set the AutoScaleMode property to Dpi then the designer will write the current system DPI into the designer.cs file in the AutoScaleDimensions property for the form. When the designer is used on a system with a different DPI, this information will be used to rescale the form and the designer can be used at a different DPI.
When I tried other scaling modes, this didn't seem to work well. 'None' meant that controls wouldn't scale at runtime, 'Font' seemed to suffer from rounding errors and when the display settings DPI changed, the control sizes could change slightly causing errors.
I also found that for UserControls that are added to forms it is best to set their AutoScaleMode to Inherit. If you use Dpi, then the controls on it get re-scaled twice and will end up being laid out incorrectly.
I came up with the guidelines above after a few hours of experimentation and internet searching where I found the following two articles:
Automatic scaling in Windows Forms
and:
Child controls on a UserControl may get clipped in a system with a lower Font Dpi
I don't think setting your dpi to a different value permanently will help you. The problem is that there are problems when you change the dpi, i.e. the form layout you have isn't able to deal with different dpi's.
I don't have an absolute solution for you, except that you should test with different dpis and see if it produces problems with the form display. It isn't hard to work out what causes problems and you'll learn what to avoid fairly quickly.

Silverlight device independent coordinates

I've written a Silverlight application that does not scale when I change from 96 dpi to 120 dpi The text and graphics does not change physical size on the screen as measured by a ruler. I've read in various places that Silverlight should adjust to changes in DPI configuration. The non-Silverlight portion of the web page scales as expected. It seems as if the Silverlight plugin is using Pixels as the unit of measure rather than 1/96 inch. What's the explanation?
I'm running Windows XP and setting the DPI in Wndows XPdesktop properties / settings / general / DPI Setting.
This wont help if you're after measurable coordinates (ie: displaying an accurate ruler in your app), but if you want your UI to be resolution independent, you can wrap it with a ViewBox, which is part of the Silverlight Toolkit

Resources