Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Tips & Tricks: How to Get the Screen Resolution in Silverlight?

0.00/5 (No votes)
29 Aug 2010 1  
The code describes how to get the Client's Screen Resolution in Silverlight.

In this tips and tricks, I will show you the steps by which you can get the Screen Resolution of Client's PC in Silverlight Application. It is quite simple. You have to just call the HTML DOM object to receive the handle of the screen and from that, you can easily get the Screen Resolution.

To get the Screen Resolution, you need to get the handle of the Window and you can get it from:

System.Windows.Browser.HtmlPage.Window;

Now from that object, you can call the Eval() with proper parameter "screen.width" or "screen.height" to get the Screen Width or Screen Height respectively. Those will give you the Screen Resolution. Suppose, you received Width = 1024 and Height = 768, means your Screen Resolution is: 1024 x 768.

Here is the complete code:

using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
 
namespace SilverlightScreenResolutionDemo
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            string Width = HtmlPage.Window.Eval("screen.width").ToString();
            string Height = HtmlPage.Window.Eval("screen.height").ToString();
            MessageBox.Show(string.Format("Current resolution : {0} X {1}", Width, Height));
        }
    }
}

From the above code, you can easily understand how we are collecting the Screen Resolution of the user's screen. Hence, use it whenever you need.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here