0

Based off this previous post Draw on the screen without a form, I am trying to draw a multi line string to the screen using GDI+. It is slow enough to where I can see it draw across line by line. Am I just using the wrong API? I am trying to make a simple browser layout engine in C#, if that matters.

using System;
using System.Drawing;
using System.Runtime.InteropServices;

[DllImport("User32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);

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

static void Main(string[] args)
{
     IntPtr desktop = GetDC(IntPtr.Zero);
    
     using (Graphics g = Graphics.FromHdc(desktop))
     {
          g.DrawString(multiLineText, new Font(FontFamily.GenericSansSerif, 20), Brushes.White, new RectangleF(0, 0, 1920, 1280));
     }
            
     ReleaseDC(IntPtr.Zero, desktop);
}
Tim
  • 150
  • 9
  • For some reason I thought it was the other way around. Thanks, I’ll give it a try. – Tim Dec 30 '21 at 23:40
  • 1
    Try [TextRenderer](https://stackoverflow.com/a/23230570/11683)? – GSerg Dec 30 '21 at 23:43
  • @Charlieface Drawing from OnPaint only applies to drawing on your own form. It makes no sense for drawing on desktop. – GSerg Dec 31 '21 at 08:33
  • @GSerg Ah fair enough wasn't reading properly. Still `ReleaseDC` should be in a `finally`. Also the `Font` should be disposed. – Charlieface Dec 31 '21 at 10:22

0 Answers0