【问题标题】:GetFontData returns -1 (GDI_ERROR) in ASP.NET App but not in console app. What could cause this?GetFontData 在 ASP.NET 应用程序中返回 -1 (GDI_ERROR),但在控制台应用程序中不返回。什么可能导致这种情况?
【发布时间】:2011-09-02 17:11:27
【问题描述】:

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

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

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 中添加了有关此问题的 cmets,其中出现错误(搜索 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 版本运行良好,如果我们找不到任何解决方案,我们很有可能会切换到该版本,但我真的很好奇可能导致这种情况的原因。

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

【问题讨论】:

    标签: c# asp.net pinvoke 32bit-64bit pdfsharp


    【解决方案1】:

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

    GDI+ 函数和类不是 支持在 Windows 中使用 服务。尝试使用这些 Windows 中的函数和类 服务可能会产生意外 问题,例如服务减少 性能和运行时异常或 错误。

    在 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;
    ...
    

    这就是为什么 PDFSharp 的 GDI+ 构建无法在服务中运行,而 WPF 构建却如您在问题中所说的那样运行。

    【讨论】:

    • PDFsharp 可以使用 GDI+ 或 WPF。问题中已经说过 WPF 构建工作正常:“PDFSharp 的 WPF 构建工作得很好顺便说一句”。感谢您提供的信息,我不知道服务中不支持 GDI+ 功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    相关资源
    最近更新 更多