0

I'm trying to draw on my screen with c# but it only seems to draw e few pixels. Also when I log

   Console.WriteLine(Screen.PrimaryScreen.Bounds.Width);
   Console.WriteLine(Screen.PrimaryScreen.Bounds.Height);

It loggs: 1620, 1080 which is half my screen size...

Here is my code with which I'm trying to draw:

 [DllImport("User32.dll")]
 public static extern IntPtr GetDC(IntPtr hwnd);
 [DllImport("User32.dll")]
 public static extern void ReleaseDC(IntPtr hwnd, IntPtr dc);

 static void Main(string[] args) {
    Console.WriteLine(Screen.PrimaryScreen.Bounds.Width);
    Console.WriteLine(Screen.PrimaryScreen.Bounds.Height);

    IntPtr desktopPtr = GetDC(IntPtr.Zero);
    Graphics g = Graphics.FromHdc(desktopPtr);
    SolidBrush b = new SolidBrush(Color.White);
    g.FillRectangle(b, new Rectangle(0, 0, 16000, 16000));
}

Hope someone can help me, Elias :)

Elias
  • 3,592
  • 2
  • 19
  • 42
  • Are you possibly using display scaling (HiDPI) in Windows? This sounds like your program isn't HiDPI aware and gets confused there. – AKX Nov 22 '18 at 08:57
  • Yes, my scaling was set to 200% by default which would exactly fit the 1/2 of the resolution... and how am I going to fix that now? – Elias Nov 22 '18 at 09:05
  • https://stackoverflow.com/questions/23551112/how-can-i-set-the-dpiaware-property-in-a-windows-application-manifest-to-per-mo seems relevant. (There's also https://stackoverflow.com/questions/4075802/creating-a-dpi-aware-application if that doesn't do the trick.) – AKX Nov 22 '18 at 10:33
  • @AKX Hm I've already seen those but I don't have any forms... just the `main` function and some threads... I'm not sure what to do :( – Elias Nov 22 '18 at 11:54
  • The first linked answer should be relevant even without threads, as you're using GDI/GDIplus to draw. – AKX Nov 22 '18 at 11:55
  • I'm pretty overwhelmed by this and can't really seem to find out what to change in my code. I may need to look further into c# to understand why and what to do :/ But thank you for your effort. – Elias Nov 22 '18 at 12:03
  • In general you shouldn't be writing directly to the screen anyway. What are you trying to do in the end? – AKX Nov 22 '18 at 15:10

0 Answers0