1

I am creating a custom console in GDI+ and C# and I am drawing each character in the buffer individually (loops rows and columns). Now the default screen size for my console contains 1600 characters (80x20) and it ignores (doesn't draw) null characters, but after I type about 150 characters it starts to freeze and drawing slows down. I'm drawing by overriding the OnPaint method and using a timer to call this.Invalidate() and this.Update() but even if I disable the timer and put it after the code that inserts characters it still has a slow drawing speed.

My drawing code is this

for (int l = topLine; l <= (this.Height / charSize.Height) + topLine; l++)
    for (int c = 0; c < bufferSize.Width; c++)
        g.DrawString(buffer[l][c].Char, this.Font, Brushes.White, buffer[l][c].Position);

that code isn't exactly what I have but I just simplified it.

  • 1
    Sure, drawing one character at a time will be very slow indeed. Get rid of the inner for() loop and draw an entire line instead. Get the same effect as your code is trying to emulate by using a fixed-pitch font, like Consolas or Century New. – Hans Passant Jun 08 '13 at 11:32
  • The problem is that each character has different ForeColor/BackColor values so I can't draw the whole line at once :\ – Bryce Gough Jun 08 '13 at 12:25
  • 1
    You'll have to write smarter code, at least subdivide the string to get all the characters with the same attributes so you can paint them all with a single DrawString call. With high odds that the entire line is a match. Do consider using RichTextBox instead. – Hans Passant Jun 08 '13 at 12:27
  • I will try that, but just wanted to question on how actual console windows (or windows like putty or cygwin) draw their characters to make it so responsive? – Bryce Gough Jun 08 '13 at 15:38
  • 1
    You'd have to to break into a vault in Redmond to see that. But you can safely assume that console windows don't draw a single character at a time either. – Hans Passant Jun 08 '13 at 15:51
  • I just opened to console outside of visual studio and it seems that the freezing doesn't happen when its not debugging. I filled up the whole window with minimal freezing. – Bryce Gough Jun 09 '13 at 02:31

0 Answers0