Click here to Skip to main content
15,881,381 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.67/5 (7 votes)
29 Aug 2010CPOL 32.1K   3   6
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:

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralReason for my vote of 5 good article Pin
Nikhil_S9-Feb-12 0:57
professionalNikhil_S9-Feb-12 0:57 
GeneralWarning: usually, you do <b>not</b> want to know the screen ... Pin
Pablo Aliskevicius6-Feb-12 3:13
Pablo Aliskevicius6-Feb-12 3:13 
GeneralReason for my vote of 4 Nice and simple one Pin
Lakamraju Raghuram6-Feb-12 0:00
Lakamraju Raghuram6-Feb-12 0:00 
GeneralReason for my vote of 3 good Pin
Vishnu Rana2-Nov-10 0:29
Vishnu Rana2-Nov-10 0:29 
GeneralReason for my vote of 5 Thanks for sharing such cool tips Pin
Member 747486028-Sep-10 18:07
Member 747486028-Sep-10 18:07 
GeneralReason for my vote of 5 Thank you for sharing this tip. Pin
linuxjr29-Aug-10 7:11
professionallinuxjr29-Aug-10 7:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.