title: Direct2D / GDI+ and slow Windows forms drawing - What can be done?
body: <p>I'm working a lot with Visual Studio 2008, .NET C# 2.0-3.5 and Windows Forms and I have noticed, like many before me, that GDI+ is extremely slow in drawing Controls. <strong>Note</strong> that I do not deal with images (JPG, GIF etc) very much. Images are only as icons in certain places. This is actually Controls/Forms/etc that are slow to draw.</p>
  
  <p>The issue is that you can see Controls being drawn and it can take several seconds for a seemingly easy set of Controls to be drawn. Ie, its lagging and horrible. </p>
  
  <p>I have made tests where I just put a number of Labels (40-50) on a form, hitting F5 to run and have to wait for them to be drawn. Again, lag and not a very nice experience.</p>
  
  <p>So, then there is WPF that might address this problem, but I/we are not ready to move to WPF. So I'm looking around for workarounds or fixes and I stumbled upon Direct2D, and when reading on that some other libraries.</p>
  
  <p><strong>Put Im a tad puzzled and thus these questions:</strong></p>
  
  <p><strong>1)</strong> 
  First of, what I want is a fairly neat and simple way to just <em>replace</em> GDI+ with something faster and hardware accelerated approach. <em>Is it possible to do that without going over to WPF and without having to rewrite all my Windows Forms code?</em></p>
  
  <p>Whenever I read anything on Direct2D I see long blocks of usually horrible C++ code, telling me on how to manually write code to for drawing. I do not want that.</p>
  
  <p><strong>2)</strong> 
  While reading on the net, I stumbled upon SlimDX, but I cannot figure out how to use it (and I admit, I havent tried very much as of writing). Lets say I already have a GUI-application (Windows Forms, standard C# code) - can I somehow use SlimDX (or something like it) to just "replace" GDI+ without too much rewriting?</p>
  
  <p>My problem is I cannot find any examples or such telling me if it is possible to use SlimDX, Direct2D or other similiar things in my already-created Windows Forms software, and if it is possible - how to do it.</p>
  
  <p>Hope Im not too fuzzy =)</p>
  
  <p><strong>==EDIT== 2010-09-22</strong></p>
  
  <p>I have made some tests in my real app, and isolated one of the slow things to this:</p>
  
  <p>When I add text to some Labels in a UserControl, the Controls resize themselves to fit the text. For example, the containing GroupControl adapts a bit to the size of the text that was just added to the .Text-property of the Labels. </p>
  
  <p>There are about 10 Label controls. The first time the labels are updated, and thus sizes are changed, the whole process takes about 500 ms. The second time the labels are updated, and <em>no</em> size changes, it takes about 0 ms.</p>
  
  <p><strong>==EDIT 2== 2010-09-22</strong></p>
  
  <p>Found one of the slow-downs. Then adding a String to the Text-property it is slow if the text that is being added differs in string <em>length</em> from the text that was there before the update.</p>
  
  <p>Im using DevExpress libraries, and the LabelControls can be set to AutoSizeMode. If I set that to "None" then the lag will go away when adding text that differs in length from the previous text. I guess this problem will be the same for the normal Label-control as it also has a AutoSize = true/false setting.</p>
  
  <p>However, its a "workaround" but still proves my point - its really slow when resizing which is pretty lame.</p>