21 January 2014

Generate the barcode in C# (String to Image in C# )

Hi Friends,

Last day on https://stackoverflow.com one user ask two questions ( one and second) how to generate the barcode in C# like the below image -


he has generated the barcode(only stripe) using the font (free 3 of 9) and string "123456" in string now he wanted to merge them like the above image. and finally he want to output as an image so that he can display that of window form / web page or he can save that for future reference.

well its really interesting task, isn't it??

I tried it at my end and finally got a solution and suggested him the same solution and finally he accepted and appreciated the solution. Use the below code that's it.

 //Set the font style of output image
Font barCodeF = new Font("Free 3 of 9", 45, FontStyle.Regular, GraphicsUnit.Pixel);
Font plainTextF = new Font("Arial", 20, FontStyle.Regular, GraphicsUnit.Pixel);


public Image stringToImage(string inputString)
{
  //remove the blank space after and before actual text
  string text = inputString.Trim();

  Bitmap bmp = new Bitmap(1, 1); 
  Graphics graphics = Graphics.FromImage(bmp);

  
  
  int barCodewidth = (int)graphics.MeasureString(text, barCodeF).Width;
  int barCodeHeight = (int)graphics.MeasureString(text, barCodeF).Height;

  int plainTextWidth = (int)graphics.MeasureString(text, plainTextF).Width;
  int plainTextHeight = (int)graphics.MeasureString(text, plainTextF).Height;

  
  
  //image width 
  if (barCodewidth > plainTextWidth)
  {
    bmp = new Bitmap(bmp,
                     new Size(barCodewidth, barCodeHeight + plainTextHeight));
  }
  else
  {
    bmp = new Bitmap(bmp,
                     new Size(plainTextWidth, barCodeHeight + plainTextHeight));
  }
  graphics = Graphics.FromImage(bmp);

  
  
  //Specify the background color of the image
  graphics.Clear(Color.White);
  graphics.SmoothingMode = SmoothingMode.AntiAlias;
  graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

  

  //Specify the text, font, Text Color, X position and Y position of the image
  if (barCodewidth > plainTextWidth)
  {
    //bar code strip
    graphics.DrawString(text,
                        barCodeF,
                        new SolidBrush(Color.Black),
                        0,
                        0);
    
    // plain text
    graphics.DrawString(text,
                        plainTextF, 
                        new SolidBrush(Color.Black),
                        (barCodewidth-plainTextWidth)/2,
                        barCodeHeight);
  }
  else
  {
    //barcode stripe
    graphics.DrawString(text,
                        barCodeF,
                        new SolidBrush(Color.Black),
                        (plainTextWidth - barCodewidth) / 2,
                        0);

    //plain text
    graphics.DrawString(text,
                        plainTextF,
                        new SolidBrush(Color.Black),
                        0,
                        barCodeHeight);
  }

  
  
  graphics.Flush();
  graphics.Dispose();

  
  //if you want to save the image  uncomment the below line.
  //bmp.Save(@"d:\myimage.jpg", ImageFormat.Jpeg);

  return bmp;
}
use this code.  Pass the string in this method and it will return you the barcode image.  Remember you must have installed the free 3 of 9 font in you systems to generate the bar code. You can download the free3 of 9 font from the given linke. Download Free3of9 Font.

This is the output of the above code when I pass string as "NRL9685325PR".


You can download the sample from here.  Download BarCodeSample .

Happy Coding and Sharing.. :)

2 comments:

  1. My Tactics: Generate The Barcode In C (String To Image In C ) >>>>> Download Now

    >>>>> Download Full

    My Tactics: Generate The Barcode In C (String To Image In C ) >>>>> Download LINK

    >>>>> Download Now

    My Tactics: Generate The Barcode In C (String To Image In C ) >>>>> Download Full

    >>>>> Download LINK Wa

    ReplyDelete
  2. Thank You and I have a super give: Whole House Reno house renovation process

    ReplyDelete