0

I am trying to draw to the screen directly in C# via the Graphics object retrieved from Graphics.FromHwnd(IntPtr.Zero) but I am limited to the primary monitor for some reason.

Upon inspecting the Graphics object, I found that the VisibleClipBounds are limited to my first monitors resolution.

I have two 1920x1080 monitors, so, I guess, that property has to be 3840x1080.

Is there a way to solve my issue?

The code is really simple, I have a wrapper class that looks like this:

public class ScreenCropperDrawer
{
    private static Graphics screenGraphics;

    public static void FillRectangle(Brush brush, Rectangle rect)
    {
        if (screenGraphics == null)
        {
            screenGraphics = Graphics.FromHwnd(IntPtr.Zero);
        }
        screenGraphics.FillRectangle(brush, rect);
    }
}
Michael
  • 548
  • 6
  • 23
  • "two 1920x1080 monitors, so, I guess, that property has to be 3840x2160." Why would you expect both dimensions to double? Are both of your monitors simultaneously side-by-side and one-above-the-other, somehow breaking the rules of physics and giving you four times the screen *area* of one monitor? – Damien_The_Unbeliever Feb 28 '19 at 13:52
  • whoops, my bad. changed that. – Michael Feb 28 '19 at 13:55
  • Hmm, I'm thinking.. if the user has a 1920x1080 monitor and a 1280x1024 monitor, you can't properly that using a rectangle. 3200x1080 isnt good, but 1920x2104 isn't either. So i think you'll need at least two calls for that. – Caramiriel Feb 28 '19 at 14:05
  • I am more interested in **getting the `Graphics` object that is not limited to one monitor after calling `Graphics.FromHwnd(IntPtr.Zero)`** rather that determining the rectangle in which my desktop would fit. Thank you! – Michael Feb 28 '19 at 14:12
  • 1
    Possible duplicate of [Get DeviceContext of Entire Screen with Multiple Montiors](https://stackoverflow.com/questions/576476/get-devicecontext-of-entire-screen-with-multiple-montiors) – Mgetz Feb 28 '19 at 14:16
  • @Mgetz, I have already tried the solution in that question. It did not work. – Michael Feb 28 '19 at 14:42
  • 1
    The `DeviceName`, returned by the Screen class (for each display), can be used as the `lpszDriver` parameter of GDI+ [CreateDC](https://learn.microsoft.com/en-us/windows/desktop/api/wingdi/nf-wingdi-createdcw). It will return the hDC of the display. `Graphics.FromHdc` can use this handle. See also here: [Using SetWindowPos with multiple monitors](https://stackoverflow.com/a/53026765/7444103) about displays and virtualscreen. – Jimi Feb 28 '19 at 16:14

0 Answers0