views:

423

answers:

2

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?

A: 

In order for the people to be able to help you, I suggest:

  1. Put a simpler code sample that breaks down (using concrete values for fonts and the rest)
  2. Remove the extra g.Dispose() from the code sample
  3. Tell us which part of the code actually throws the exception
Igor Brejc
A: 

I wasn't able to resolve this issue using the barcode font, so I'm just printing my labels with ZPL now instead.

Ciaran