Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 29 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kunal Chowdhury (@kunal2383)
Team Leader
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer 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 Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.
 
Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralReason for my vote of 5 good article Pinmembernikhi _singh9-Feb-12 1:57 
GeneralWarning: usually, you do <b>not</b> want to know the screen ... PinmemberPablo Aliskevicius6-Feb-12 4:13 
GeneralReason for my vote of 4 Nice and simple one PinmemberLakamraju Raghuram6-Feb-12 1:00 
GeneralReason for my vote of 3 good PinmemberVishnu Rana2-Nov-10 1:29 
GeneralReason for my vote of 5 Thanks for sharing such cool tips PinmemberMember 747486028-Sep-10 19:07 
GeneralReason for my vote of 5 Thank you for sharing this tip. Pinmemberlinuxjr29-Aug-10 8:11 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 29 Aug 2010
Article Copyright 2010 by Kunal Chowdhury (@kunal2383)
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid