2009-06-30 88 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();

使用聲明將已配置它。