C# GDI+;使用DrawString()时出现异常

C# GDI+;使用DrawString()时出现异常,c#,.net,winforms,exception,gdi+,C#,.net,Winforms,Exception,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&g

尝试使用条形码字体创建条形码图像时出错。这在生产中发生,但在开发中没有

创建条形码的方法是:

/// <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;
    }
    }
}
我无法找出问题的原因,但从谷歌搜索结果来看,似乎是对非托管代码的调用导致了问题,但我还没有找到解决方案


有人吗?

您能试着删除下面的行吗

g、 处置()


使用语句将已对其进行处理。

您是否尝试过使用不同的字体来查看是否有任何区别?您在图形对象上调用了两次.Dispose(),顺便说一句。是的,我尝试过使用不同的字体(当前的字体是条形码字体),并且使用了另一种(普通)嵌入字体,还引用了一种系统字体,结果仍然相同。g.Dispose()已经删除,但仍然是相同的应用程序是通过ClickOnce部署的-我想知道这是否是问题的一部分,所以你是说即使使用不同的字体,问题仍然存在?嗯,你用它画画的时候确保字体没有被处理吗?这同样适用于_foregroundBrush。这是在多台计算机上发生的还是仅在一台计算机上发生的?(生产)这是在多台开发人员计算机上进行的,还是仅在一台开发人员计算机上进行的?你解决过这个问题吗?
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