编程语言
首页 > 编程语言> > c# – GetFontData在ASP.NET App中返回-1(GDI_ERROR)但在控制台应用程序中不返回.什么可能导致这个?

c# – GetFontData在ASP.NET App中返回-1(GDI_ERROR)但在控制台应用程序中不返回.什么可能导致这个?

作者:互联网

我们在其中一个Web应用程序中使用PDFSharp(GDI构建).在一个PDF导出器中,我们使用非系统truetype字体,它在我们的开发环境中就像一个魅力,但是当我们在生产中运行它时会崩溃.

我们的开发和生产之间的关键区别(我认为)是我们的生产服务器在Windows Server 2008 64bit上运行,而我们的开发运行在2008 32bit.我写了一个很小的测试程序来调试.

try
{
    new XFont("ocrb10", 10, XFontStyle.Regular, new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always));
}
catch (Exception exc) { Console.WriteLine(exc.StackTrace); }

错误消息是InvalidOperationException:内部错误.无法检索字体数据.

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)

我从源代码构建了PDFSharp并添加了一些调试代码,以便了解发生了什么.问题是对GetFontData的pinvoke调用返回-1(GDI_ERROR). PdfSharp作者在FontData.cs中添加了关于此的注释,其中发生错误(搜索GDI_ERROR)但他也找不到合适的解决方案.

// 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);

现在,我遇到的问题是,当我将代码作为控制台应用程序运行时,我无法在任何环境中重现此错误.我已经尝试为应用程序池打开和关闭WOW64,并且我尝试在我自己的凭据下运行应用程序池,以防有任何与权限相关的问题但无济于事.

PDFSharp的WPF构建工作非常顺利,如果我们找不到任何解决方案,我们很可能会切换到那个,但我真的很好奇可能导致这种情况的原因.

谁能帮助我进一步调试步骤?在PInvokes中,当在IIS / ASP.NET中运行时,与控制台应用程序相比,环境有何不同?

解决方法:

开发人员通常依赖GDI来检索字体指标,但是according to MSDN

GDI+ functions and classes are not
supported for use within a Windows
service. Attempting to use these
functions and classes from a Windows
service may produce unexpected
problems, such as diminished service
performance and run-time exceptions or
errors.

在FontData.cs中,我发现了以下内容:

#if GDI
100    /// <summary>
101    /// Create the font image using GDI+ functionality.
102    /// </summary>
103    void CreateGdiFontImage(XFont font, XPdfFontOptions options/*, XPrivateFontCollection privateFontCollection*/)
104    {
105      System.Drawing.Font gdiFont = font.RealizeGdiFont();
106      NativeMethods.LOGFONT logFont;
...

这就是为什么GFP构建的PDFSharp不能在服务中工作,而WPF构建确实可以像您在问题中所说的那样工作.

标签:c,asp-net,32bit-64bit,pinvoke,pdfsharp
来源: https://codeday.me/bug/20190621/1254628.html