GDI+ 使用 DrawString() 时出现异常
我在尝试使用条形码字体创建条形码图像时遇到错误。 这种情况在生产中发生,但在开发中却没有。
创建条形码的方法是:
/// <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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试删除以下行
g.Dispose();
using 语句将已经处理它。
Could you try removing following line
g.Dispose();
using statement will dispose it already.