title: System.Windows.Media.DrawingVisual.RenderOpen() erroring after a time
body: <p>Use Case: I'm using WPF to dynamically overlay text on images within a web context. </p>
  
  <p>Solution: 
  I'm using a <code>DrawingContext</code> from a <code>DrawingVisual</code> (wrapped in a <code>using</code> statement) to draw the original bitmap and overlay text. </p>
  
  <p>Problem: After a few thousand requests the <code>[DrawingVisual].RenderOpen()</code> method starts refusing to execute and throws the following exception:</p>
  
  <pre><code>The system cannot find the file specified System.ComponentModel.Win32Exception UInt16 RegisterClassEx(WNDCLASSEX_D) at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
  at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
  at System.Windows.Media.MediaContextNotificationWindow..ctor(MediaContext ownerMediaContext)
  at System.Windows.Media.MediaContext..ctor(Dispatcher dispatcher)
  at System.Windows.Media.MediaContext.From(Dispatcher dispatcher)
  at System.Windows.Media.Visual.VerifyAPIReadWrite()
  at System.Windows.Media.DrawingVisual.RenderOpen()
  at ...
  </code></pre>
  
  <p>Temp fix: The only fix for this is to recycle the IIS application pool on the server which fixes the problem for a few hours.</p>
  
  <p>Question: Anyone have a thought on the cause of this issue (leaks, threading, unmanaged code etc)? Is there any other way of getting text on an image avoiding the drawing visual? Perhaps writing geometry to a bitmap and overlaying the bitmaps directly?</p>