Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,836 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,667 people online right now. Registration is fast and FREE... Join Now!




Getting the Screen Resolution

 
Reply to this topicStart new topic

Getting the Screen Resolution

gbertoli3
3 Oct, 2008 - 08:59 PM
Post #1

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
I am trying to get the Screen Resolution using the System.Management Namespace

This is what I am trying:
csharp

System.Management.SelectQuery query = new System.Management.SelectQuery("Win32_VideoController");
System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(query);
foreach (System.Management.ManagementObject ManageObject in searcher.Get())
{
String ResX = ManageObject["CurrentHorizontalResolution"].ToString();
String ResY = ManageObject["CurrentVerticalResolution"].ToString();
resolutionLabel.Text = "Resolution: " + ResX + " x " + ResY;
}

Then I get this Error:
QUOTE(Compiler Error)

Object reference not set to an instance of an object.


What am I doing wrong?

Could you tell me how to do it with and without the System.Management, Please?

Thanks
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Getting The Screen Resolution
3 Oct, 2008 - 09:44 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You have a couple of options, you could use SystemInformation.PrimaryMonitorSize, or you could use Screen.PrimaryScreen Property like so

csharp

int deskHeight = Screen.PrimaryScreen.Bounds.Height;
int deskWidth = Screen.PrimaryScreen.Bounds.Width;


You could also use something like this

csharp

public string GetScreenResolution()
{
int width = Screen.Width / Screen.TwipsPerPixelX;
int height = Screen.Height / Screen.TwipsPerPixelY;
return "Screen Resolution is " + width + " x " + height;
}


or even

csharp

/// <summary>
/// Gets the current screen resolution
/// </summary>
/// <returns>Screen resolution as Size</returns>
privatestring GetScreenRes()
{
int iWidth = Screen.PrimaryScreen.Bounds.Width;
int iheight = Screen.PrimaryScreen.Bounds.Height;

return "Resolution is " + iWidth + " x " + iheight;

}


Hope that helps smile.gif
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Getting The Screen Resolution
3 Oct, 2008 - 10:09 PM
Post #3

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
OK Thanks

I thought you had to use the Management Namespace
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Getting The Screen Resolution
3 Oct, 2008 - 10:14 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
No problem, glad I could help smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 04:11PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month