Search code examples
c#winformsgraphicsautoscrollonpaint

Graphics.Draw*, AutoScroll & Culling


I've implemented a custom control in C#/Winforms which does things like syntax highlighting & autocomplete. I'm using AutoScroll to manage scrolling and it works nicely.

Currently I have not optimized at all (sure optimization is important, but I'm doing that last; functionality is what I'm after first), I am rendering huge documents, and each keypress will re-parse the affected line to make sure syntax highlighting is consistent.

Right now in my big meaty paint method, I am painting every string, keyword, etc, even if it is outside of the clip region. But regardless of how big the document is & how many combinations of keywords/highlighted bits & pieces I have, it still runs bloody fast with not much memory & CPU overhead.

So my question - do the Graphics.Draw* methods do any kind of culling? Eg: If the AutoScrollPosition is way down the document & I Graphics.DrawString(insert some coordinates outside the draw region), is any actual work being done? Also note I'm running VS on Win 7 inside a VM, and it is still running fast. Not that it's an issue now, but it would be nice to keep in mind for later when it comes to the optimization phase. :D

Cheers, Aaron


Solution

  • From personal experience writing games that use Graphics.Draw* methods, you will notice a speed increase if you perform your own bounds checking before calling the drawing methods.

    Attempting to draw things offscreen is faster than drawing things onscreen, but its still noticeably slower than not drawing them at all.