尝试使用条形码字体创建条形码图像时出错。这种情况发生在生产中,但不是在开发中。
创建条形码的方法是:
/// <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;
}
}
}
这个代码在循环中被调用,因为需要几个条形码。在开发环境中,它工作正常但在实时(在winforms应用程序中使用.net 3.5 SP1的Win XP Pro上),创建了2个条形码,并在第3次引发异常。
提出异常&amp;堆栈跟踪是:
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 :(得分:0)
您可以尝试删除以下行
g.Dispose();
using语句将把它处理掉。