GDI+ 使用 DrawString() 时出现异常

发布于 2024-07-25 15:22:17 字数 2181 浏览 2 评论 0原文

我在尝试使用条形码字体创建条形码图像时遇到错误。 这种情况在生产中发生,但在开发中却没有。

创建条形码的方法是:

/// <summary>
/// Create a barcode image by writing out a string using a barcode font and save it
/// </summary>
/// <param name="barcodeText">The text string of the barcode</param>
/// <param name="saveLocation">Where to save the file to</param>
/// <param name="font">The barcode font</param>
/// <param name="imageFormat">The image format</param>
private void CreateBarcodeImage(string barcodeText, string saveLocation, System.Drawing.Font font, ImageFormat imageFormat)
{
    // Draw the barcode image
    using (Bitmap bmp = new Bitmap(500, 75))
    {
    try
    {
        using (Graphics g = Graphics.FromImage(bmp))
        {
        g.Clear(_backgroundColour);
        g.DrawString(barcodeText, font, _foregroundBrush, 10, 0);
        bmp.Save(saveLocation, imageFormat);
        g.Dispose();
        }
    }
    catch (Exception ex)
    {
        Log.ErrorException("Exception in LabelPrinter.CreateBarcodeImage()", ex);
        throw;
    }
    }
}

由于需要多个条形码,因此在循环中调用此代码。 在开发环境中它工作正常,但在实时环境中(在 Win XP Pro 上,在 winforms 应用程序中使用 .net 3.5 SP1),创建了 2 个条形码,并且在第三次时引发异常。

引发的异常 & 堆栈跟踪是:

An unhandled exception has occurred Attempted to read or write protected memory. This is often an indication that other memory is corrupt. 
at System.Drawing.SafeNativeMethods.Gdip.GdipMeasureString(HandleRef graphics, String textString, Int32 length, HandleRef font, GPRECTF& layoutRect, HandleRef stringFormat, GPRECTF& boundingBox, Int32& codepointsFitted, Int32& linesFilled) 
at System.Drawing.Graphics.MeasureString(String text, Font font, SizeF layoutArea, StringFormat stringFormat) 
at System.Drawing.Graphics.MeasureString(String text, Font font) 
at Srcl.WasteTrak.Gui.Documents.LabelPrinter.CreateBarcodeImage(String barcodeText, String saveLocation, Font font, ImageFormat imageFormat) 
in c:\scc\SRCL\SRCL.WasteTrak\SRCL.WasteTrak.Gui\Documents\LabelPrinter.cs:line 60 

我无法找出导致问题的原因,但从 Google 搜索来看,似乎是调用非托管代码导致的,但我还没有找到解决方案。

任何人?

I'm getting an error when trying to create a barcode image using a barcode font. This is happening in production but not in dev.

The method creating the barcode is:

/// <summary>
/// Create a barcode image by writing out a string using a barcode font and save it
/// </summary>
/// <param name="barcodeText">The text string of the barcode</param>
/// <param name="saveLocation">Where to save the file to</param>
/// <param name="font">The barcode font</param>
/// <param name="imageFormat">The image format</param>
private void CreateBarcodeImage(string barcodeText, string saveLocation, System.Drawing.Font font, ImageFormat imageFormat)
{
    // Draw the barcode image
    using (Bitmap bmp = new Bitmap(500, 75))
    {
    try
    {
        using (Graphics g = Graphics.FromImage(bmp))
        {
        g.Clear(_backgroundColour);
        g.DrawString(barcodeText, font, _foregroundBrush, 10, 0);
        bmp.Save(saveLocation, imageFormat);
        g.Dispose();
        }
    }
    catch (Exception ex)
    {
        Log.ErrorException("Exception in LabelPrinter.CreateBarcodeImage()", ex);
        throw;
    }
    }
}

This code is being called in a loop as several barcodes are needed. In the dev environment it works fine but in live (on Win XP Pro using .net 3.5 SP1 in a winforms app), 2 barcodes are created and the exception is raised on the 3rd time.

The exception being raised & stack trace is:

An unhandled exception has occurred Attempted to read or write protected memory. This is often an indication that other memory is corrupt. 
at System.Drawing.SafeNativeMethods.Gdip.GdipMeasureString(HandleRef graphics, String textString, Int32 length, HandleRef font, GPRECTF& layoutRect, HandleRef stringFormat, GPRECTF& boundingBox, Int32& codepointsFitted, Int32& linesFilled) 
at System.Drawing.Graphics.MeasureString(String text, Font font, SizeF layoutArea, StringFormat stringFormat) 
at System.Drawing.Graphics.MeasureString(String text, Font font) 
at Srcl.WasteTrak.Gui.Documents.LabelPrinter.CreateBarcodeImage(String barcodeText, String saveLocation, Font font, ImageFormat imageFormat) 
in c:\scc\SRCL\SRCL.WasteTrak\SRCL.WasteTrak.Gui\Documents\LabelPrinter.cs:line 60 

I can't find out what is causing the problem but from Google searches it appears to be calls into unmanaged code cause it, but i haven't found a solution.

Anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只怪假的太真实 2024-08-01 15:22:17

您可以尝试删除以下行

g.Dispose();

using 语句将已经处理它。

Could you try removing following line

g.Dispose();

using statement will dispose it already.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文