title: GetFontData returns -1 (GDI_ERROR) in ASP.NET App but not in console app. What could cause this?
body: <p>We use PDFSharp (the GDI+ build) in one of our web applications. In one PDF exporter we're using a non-system truetype font and it works like a charm in our dev environment but crashes when we run it in production.</p>
  
  <p>The key difference (I think) between our dev and production is that our production servers are running on Windows Server 2008 64bit while our dev is running 2008 32bit. I wrote a tiny test program to debug.</p>
  
  <pre><code>try
  {
      new XFont("ocrb10", 10, XFontStyle.Regular, new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always));
  }
  catch (Exception exc) { Console.WriteLine(exc.StackTrace); }
  </code></pre>
  
  <p>The error message is InvalidOperationException: Internal error. Font data could not retrieved.</p>
  
  <pre><code>at PdfSharp.Fonts.OpenType.FontData.CreateGdiFontImage(XFont font, XPdfFontOptions options)
  at PdfSharp.Fonts.OpenType.FontData..ctor(XFont font, XPdfFontOptions options)
  at PdfSharp.Fonts.OpenType.OpenTypeDescriptor..ctor(XFont font, XPdfFontOptions options)
  at PdfSharp.Fonts.OpenType.OpenTypeDescriptor..ctor(XFont font)
  at PdfSharp.Fonts.FontDescriptorStock.CreateDescriptor(XFont font)
  at PdfSharp.Drawing.XFont.get_Metrics()
  at PdfSharp.Drawing.XFont.Initialize()
  at PdfSharp.Drawing.XFont..ctor(String familyName, Double emSize, XFontStyle style, XPdfFontOptions pdfOptions)
  </code></pre>
  
  <p>I built PDFSharp from source and added some debug code in order to understand what's happening. The problem is that the pinvoke call to <a href="http://msdn.microsoft.com/en-us/library/dd144885%28v=vs.85%29.aspx" rel="nofollow">GetFontData</a> returns -1 (GDI_ERROR). The PdfSharp author has added comments about this in <a href="http://pdfsharp.codeplex.com/SourceControl/changeset/view/51421#707114" rel="nofollow">FontData.cs</a> where the error occurrs (search for GDI_ERROR) but he didn't find a proper resolution either.</p>
  
  <pre><code>// Line 138 in FontData.cs, this GetFontData returns -1 here when 
  // running as a web application on a 64bit windows host (regardles of WOW64
  // being enabled or not)
  int size = NativeMethods.GetFontData(hdc, 0, 0, null, 0);
  </code></pre>
  
  <p>Now, the problem for me is that I am <em>unable to reproduce this error in any environment when I run the code as an console application</em>. I've tried toggling WOW64 on and off for the application pool and I've tried running the application pool under my own credentials in case there was any permission related problems but to no avail.</p>
  
  <p>The WPF build of PDFSharp works great btw and it's quite possible that we'll just switch to that if we don't find any solution but I'm really curious as to what could cause this.</p>
  
  <p>Can anyone please help me with further debugging steps? In what ways are the environment different when running in IIS/ASP.NET versus a console application when it comes to PInvokes?</p>