title: Add margin to barcode image using C#
body: <p>I'm creatin a Code39 barcode image with C# and I need to add margin to this image. </p>
  
  <p>This is the first image which I created. </p>
  
  <p><a href="https://i.stack.imgur.com/b6TVi.png" rel="nofollow noreferrer">I created it.</a></p>
  
  <p>But it should be like this.</p>
  
  <p><a href="https://i.stack.imgur.com/kUV0q.png" rel="nofollow noreferrer">It should be like this.</a></p>
  
  <p>The code is complicated. </p>
  
  <p>This code is creating barcode bars, number and title strings. </p>
  
  <pre><code>    public byte[] Code39(string code, int barSize, bool showCodeString, string title, string fontFile)
      {
          // Create stream....
          MemoryStream ms = new MemoryStream();
          FontFamilyName = "Free 3 of 9";//ConfigurationSettings.AppSettings["BarCodeFontFamily"];
          FontFileName = fontFile;//@"C:\Documents and Settings\narottam.sharma\Desktop\Barcode\WSBarCode\Code39Font\FREE3OF9.TTF";// ConfigurationSettings.AppSettings["BarCodeFontFile"];
          FontSize = barSize;
          ShowCodeString = showCodeString;
          if (title + "" != "")
              Title = title;
          Bitmap objBitmap = GenerateBarcode(code);
          objBitmap.Save(ms, ImageFormat.Png);
  
          //return bytes....
          return ms.GetBuffer();
      }
  
      public Bitmap GenerateBarcode(string barCode)
      {
          int bcodeWidth = 0;
          int bcodeHeight = 0;
          // Get the image container...
          Bitmap bcodeBitmap = CreateImageContainer(barCode, ref bcodeWidth, ref bcodeHeight);
          Graphics objGraphics = Graphics.FromImage(bcodeBitmap);
  
          // Fill the background          
          objGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bcodeWidth, bcodeHeight));
          int vpos = 0;
          // Draw the title string
          if (_titleString != null)
          {
              objGraphics.DrawString(_titleString, _titleFont, new SolidBrush(Color.Black), XCentered((int)_titleSize.Width, bcodeWidth), vpos);
              vpos += (((int)_titleSize.Height) + _itemSepHeight);
          }
          // Draw the barcode
          objGraphics.DrawString(barCode, Code39Font, new SolidBrush(Color.Black), XCentered((int)_barCodeSize.Width, bcodeWidth), vpos);
  
  
          // Draw the barcode string
          if (_showCodeString)
          {
              vpos += (((int)_barCodeSize.Height));
              objGraphics.DrawString(barCode, _codeStringFont, new SolidBrush(Color.Black), XCentered((int)_codeStringSize.Width, bcodeWidth), vpos);
          }
          // return the image...                                  
          return bcodeBitmap;
      }
  
      private Bitmap CreateImageContainer(string barCode, ref int bcodeWidth, ref int bcodeHeight)
      {
          Graphics objGraphics;
  
          // Create a temporary bitmap...
          Bitmap tmpBitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
          objGraphics = Graphics.FromImage(tmpBitmap);
  
          // calculate size of the barcode items...
          if (_titleString != null)
          {
              _titleSize = objGraphics.MeasureString(_titleString, _titleFont);
              bcodeWidth = (int)_titleSize.Width;
              bcodeHeight = (int)_titleSize.Height + _itemSepHeight;
          }
          _barCodeSize = objGraphics.MeasureString(barCode, Code39Font);
          bcodeWidth = Max(bcodeWidth, (int)_barCodeSize.Width);
          bcodeHeight += (int)_barCodeSize.Height;
          if (_showCodeString)
          {
              _codeStringSize = objGraphics.MeasureString(barCode, _codeStringFont);
              bcodeWidth = Max(bcodeWidth, (int)_codeStringSize.Width);
              bcodeHeight += (_itemSepHeight + (int)_codeStringSize.Height);
          }
          // dispose temporary objects...
          objGraphics.Dispose();
          tmpBitmap.Dispose();
          return (new Bitmap(bcodeWidth, bcodeHeight, PixelFormat.Format32bppArgb));
      }
  </code></pre>