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

Join 131,567 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,165 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
post 3 Oct, 2008 - 08:59 PM
Post #1


DIC at Heart + Code

Group Icon
Joined: 23 Jun, 2008
Posts: 1,024



Thanked 16 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

Go to the top of the page


PsychoCoder
post 3 Oct, 2008 - 09:44 PM
Post #2


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,906



Thanked 116 times

Dream Kudos: 8450

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

Go to the top of the page

gbertoli3
post 3 Oct, 2008 - 10:09 PM
Post #3


DIC at Heart + Code

Group Icon
Joined: 23 Jun, 2008
Posts: 1,024



Thanked 16 times

Dream Kudos: 950
My Contributions


OK Thanks

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

Go to the top of the page

PsychoCoder
post 3 Oct, 2008 - 10:14 PM
Post #4


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,906



Thanked 116 times

Dream Kudos: 8450

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

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 03:07AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month