Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this code i had written:
C#
private Bitmap CreateBarcode(string s)
       {
           Bitmap barcode = new Bitmap(1, 1);
           Font font = new Font("Free Font", 60, FontStyle.Regular, GraphicsUnit.Point);

           Graphics graphic = Graphics.FromImage(barcode);

           SizeF size = graphic.MeasureString(s, font);

     barcode = new Bitmap(barcode,size.ToSize());// here is error/ here is error;      

           graphic = Graphics.FromImage(barcode);
           graphic.Clear(Color.White);

           graphic.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
           graphic.DrawString(s, font, new SolidBrush(Color.Black), 0, 0);
           graphic.Flush();
           font.Dispose();
           graphic.Dispose();

           return barcode;

       }


What I have tried:

i had change the size parameter but it does not work
Posted
Updated 29-Feb-16 21:51pm

I pasted your code into my app, and the only way I could get teh error you do is you pass it an empty string:
C#
Bitmap b = CreateBarcode("");

Which would give you a size of (0,0) and that would cause the error.

So check your method argument for null, all whitespace, or empty - and return a blank bitmap.
 
Share this answer
 
OriginalGriff was a bit faster than me.. but I have a suggestion on how to otherwise improve your code: Instead of explicitly disposing objects, use using-blocks. It will ensure that your object is being disposed "no matter what" when leaving the scope and it improves readibility (well, most people think so at least). Also, by re-assigning a second Graphics-object to your variable graphics you're not disposing the first one.
C#
private Bitmap CreateBarcode(string text)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentException(nameof(text) + " may not be null or empty.");

    Bitmap barcode;

    using (Font font = new Font("Free Font", 60, FontStyle.Regular, GraphicsUnit.Point))
    {
        using (Bitmap dummyBitmap = new Bitmap(1, 1))
        using (Graphics graphic = Graphics.FromImage(dummyBitmap))
        {
            SizeF size = graphic.MeasureString(text, font);
            barcode = new Bitmap(dummyBitmap, size.ToSize());
        }

        using (Graphics graphic = Graphics.FromImage(barcode))
        {
            graphic.Clear(Color.White);
            graphic.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
            graphic.DrawString(text, font, new SolidBrush(Color.Black), 0, 0);
            graphic.Flush();
        }
    }

    return barcode;
}
 
Share this answer
 
v2
Thanks both of you

it's solved by other way but there is other issue occurred which is regarding GDI+ error

which is

at save path


C#
Bitmap barcode = CreateBarcode(txtGenearte.Text);
barcode.Save(@"C:\barcode.gif",ImageFormat.Gif);=>A generic error occurred in GDI+.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900