2009-06-30 83 views
3

尝试使用条形码字体创建条形码图像时出现错误。这在生产中发生,但不在开发中。使用DrawString时的GDI +异常()

的方法,创建所述条形码是:

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

我无法找出是什么原因造成的问题,但谷歌从搜索这似乎是调用非托管代码的原因,但我避风港”找到了解决方案。

有人吗?

+0

您是否尝试过使用不同的字体来查看它是否有所作为?您在Graphics对象上调用.Dispose()两次,btw。 – 2009-06-30 13:31:04

回答

0

你能尝试删除以下行

g.Dispose();

使用声明将已配置它。