0

I would like to get the screen resolution (only one monitor support is fine).

I would rather avoid referencing either WinForms or WPF dlls, as outlined in all answers here, since I am in a non-UI dll. (Yes, I know it would be possible.)

Is there some way to get the resolution without those dependencies?

Actually, I would like to use this to capture screen shots in a UI testing .dll - all code samples I have found rely on knowing the screen bounds, the shortest one is:

System.Drawing.Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
    using (Graphics g = Graphics.FromImage(bitmap))
    {
        g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, bounds.Size);
    }
    bitmap.Save("test.jpg", ImageFormat.Jpeg);
}
Community
  • 1
  • 1
Andreas Reiff
  • 7,961
  • 10
  • 50
  • 104
  • You say its a non-UI dll, but its used for UI testing, so what would be the issue in linking a UI library? Do you expect the testing to run under a windows service or non-interactive session? – Ron Beyer Oct 12 '15 at 18:09
  • There should be zero issue linking a core .NET library for WinForms or WPF, but you can also get the screen resolution using Win API directly (as shown in linked question). – Eric J. Oct 12 '15 at 18:11
  • @Ron No, just trying to avoid too many references. And to find out if it is possible. – Andreas Reiff Oct 12 '15 at 18:25
  • @Eric Answer there (GetDesktopWindow/GetWindowRect) is working fine with pinvoking, thanks! – Andreas Reiff Oct 12 '15 at 18:26

0 Answers0