我正在尝试使用条形码字体创建条形码图像时遇到错误。这在生产环境中发生,但在开发环境中没有出现此问题。
创建条形码的方法是:
/// <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上使用.net 3.5 SP1在winforms应用程序中),创建了两个条形码并在第三次时引发了异常。
引发的异常和堆栈跟踪如下:
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
我找不出是什么原因导致了这个问题,但从谷歌搜索来看似乎是调用非托管代码引起的,但我还没有找到解决方案。
有人能帮忙吗?