7

I don't know the .NET framework (4.5) well enough, so here's a question I can't find an answer to.

How do I get the screen resolution of the primary screen when not working with windows forms or any other graphical environment like WPF, Silverlight, ASP.NET, etc? I'm trying to get the resolution in a class library (dll) and pass it on from there.

Does .NET have such functionality?

7
  • You can add a reference to windows forms even if you do not have a GUI interface. Is that an option? Commented Apr 28, 2013 at 21:29
  • stackoverflow.com/questions/1317235/… Commented Apr 28, 2013 at 21:39
  • @SamPlusPlus: Thanks but that doesn't work, I'm getting an error when adding the reference. Commented Apr 28, 2013 at 22:04
  • @bizzehdee: I'll give the presentationFramework a shot, thanks! Commented Apr 28, 2013 at 22:04
  • If you don't use any of the class libraries that actually care about the screen size then why would you care about it? Do avoid falling in the trap of writing "this is what my machine looks like" code. Common for new programmers, it's an exercise of sorts and console mode apps are easy, but it isn't useful to learn these things. Read Petzold to learn the core. Commented Apr 28, 2013 at 22:26

2 Answers 2

11

Although you're not working in a Winforms enviroment, you can still add a reference to it's DLLs. Adding a reference to the System.Windows.Forms.dll means that you can use:

SystemInformation.VirtualScreen.Width   
SystemInformation.VirtualScreen.Height
Sign up to request clarification or add additional context in comments.

2 Comments

+1: note that PrimaryMonitorSize is probably more suitable in this case (only matters for multi-monitor configs).
Hey, and thanks... I've tried adding the reference before but somehow it didn't work! I decided to give it one more shot and now it's working :/ Thanks for pushing me to try again!
1

Here is what you need.

It's too late but i think it will useful for another :) The answer is using P/Invoke with SystemMetric.

You can get size of your primary screen without add reference to System.Windows.Forms.dll.

http://pinvoke.net/default.aspx/Enums.SystemMetric

I don't like import a big dll to using one simple method XD

3 Comments

I'm not that sure, doesn't importing a DLL for one function impact performance even more than referencing a DLL from your application once? I mean, referencing a DLL that's already part of the .NET framework doesn't make your application more bulky than importing a DLL each time your require a function. Am I missing something?
In fact, there are many .NET method using P/invoke to call win 32 api function. You should access referencesource.microsoft.com to see that. So if you don't want to import .net dll, you can invoke win32 api directly. In this case, you just need to create SystemMetric enum with one or two value to get screen size. Then call to GetSystemMetrics function.
A P/invoke call is always a performance hit compared to a .Net library call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.