Tutorial :Huge Whitespace exists to right after drawing image. Want to get rid of it



Question:

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();  


Solution:1

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »