views:

65

answers:

1

I'm using the following codeproject to build an asp.net website and so far everything is good. My only problem is after the barcode is generated, a huge whitespace exist to the right of the barcode. I've been playing with this and am unable to resolve it.

Details below:

Link to Code Project Article: http://www.codeproject.com/KB/aspnet/AspBarCodes.aspx?msg=3543809

Copy of the Font is here: http://trussvillemethodist.web01.appliedi-labs.net/IDAutomationHC39M.ttf

          //Working Path
        string sWorkPath = "";
        sWorkPath = this.Context.Server.MapPath("");

        //Fonts
        PrivateFontCollection fnts = new PrivateFontCollection();
        fnts.AddFontFile(sWorkPath + @"\IDAutomationHC39M.ttf");
        FontFamily fntfam = new FontFamily("IDAutomationHC39M", fnts);
        Font oFont = new Font(fntfam, 18);

        // Get the Requested code sent from the previous page.
        string strCode = Request["code"].ToString();

        //Graphics
        //I don't know what to set the width to as I can't call the MeasureString without creating the Graphics object.
        Bitmap oBitmaptemp = new Bitmap(40, 100);
        Graphics oGraphicstemp = Graphics.FromImage(oBitmaptemp);
        int w = (int)oGraphicstemp.MeasureString(strCode, oFont).Width + 4;

        // Create a bitmap object of the width that we calculated and height of 100
        Bitmap oBitmap = new Bitmap(w, 100);

        // then create a Graphic object for the bitmap we just created.
        Graphics oGraphics = Graphics.FromImage(oBitmap);

        // Let's create the Point and Brushes for the barcode
        PointF oPoint = new PointF(2f, 2f);
        SolidBrush oBrushWrite = new SolidBrush(Color.Black);
        SolidBrush oBrush = new SolidBrush(Color.White);

        // Now lets create the actual barcode image
        // with a rectangle filled with white color
        oGraphics.FillRectangle(oBrush, 0, 0, w, 100);

        // We have to put prefix and sufix of an asterisk (*),
        // in order to be a valid barcode
        oGraphics.DrawString("*" + strCode + "*", oFont, oBrushWrite, oPoint);

        // Then we send the Graphics with the actual barcode
        Response.ContentType = "image/gif";
        oBitmap.Save(Response.OutputStream, ImageFormat.Gif);

        oBitmap.Dispose();
        oGraphics.Dispose();
        oBrush.Dispose();
        oFont.Dispose();
+3  A: 

The code just assumes 40 pixels per character, which is why you get a lot of image left on the right of the text. You can use the MeasureString method to measure the size of the text, and use that to create an image of the correct size:

int w = (int)oGraphics.MeasureString("*123$10.00*", oFont).Width + 4;

I noticed that you don't dispose any of the objects that you are using. The Graphics, Bitmap, SolidBrush and Font objects need to be disposed.

You might also want to consider using a GIF image instead of JPEG, it's more suited for this kind of graphics.

Guffa
Sorry, can you spell this out for me. I can't call the oGraphics or oFont because it hasn't been instantiated yet. I also move the "int w" down because its used to create the Bitmap object. The "Code" string is coming from a something.aspx?code=123$2.00
mbcrump
Your code is also implicitly convert type 'System.Drawing.SizeF' to 'int' which is not allowed. Any help is appreciated.
mbcrump
@mbcrump: You have to create another `Graphics` object first so that you can use that to call the `MeasureString`. The code creating the `Font` object can be moved up, as it doesn't depend on anything else. You use the `Code` string just as in the `DrawString` call, I just used the string that you had in your original code.
Guffa
@mbcrump: The overload used only has two parameters, the `4` is added afterwards, it's not a third parameter.
Guffa
Graphics.MeasureString returns a SizeF type. It will not compile with the way that you specified. If you can write some sample code for me with this working then it would be much appreciated. I am spinning my wheels on this.
mbcrump
@mbcrump: Oh, now I see what you mean. It returns both the width and height of the text, so you need to use the `Width` property to get the desired value. As that it a `float` you have to cast it to `int`.
Guffa
Why are you adding +4, shouldn't that be 40?
mbcrump
@mbcrump: Because you draw the text at 2,2, so I assumed that you wanted a 2 pixel margin on the right also.
Guffa
I've updated my code above with some of your initial suggestions, I still can't seem to get the barcode to generate correctly. I've added a few comments. Thanks
mbcrump
Let me also add that I can adjust the 4 to 40 and the image looks ok, but it will no longer scan.
mbcrump
@mbcrump: You measure the string without the starting and trailing asterisk, so the width that you get is too small.
Guffa