Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have generated an image for barcode in MVC but when i print this barcode then it snot getting scanned when i Scan through Scanner.

What I have tried:

I have Tried

Controller--->
barcodecs objbar = new barcodecs();
          for (int i = model.From; i <= model.To; i++)
          {
              Barcode objprod = new Barcode()
              {
                  BookId = model.From,
                  BarCode1 = objbar.generateBarcode(),
                  BarCodeImage = objbar.getBarcodeImage(objbar.generateBarcode(), Convert.ToString(model.BookId)),
              };
              model.From++;
              db.Barcodes.Add(objprod);
              db.SaveChanges();
          }
          return RedirectToAction("BarCode", "BarCode");




Barcode.cs-->
public string generateBarcode()
      {
          try
          {
              string[] charPool = "1-2-3-4-5-6-7-8-9-0".Split('-');
              StringBuilder rs = new StringBuilder();
              int length = 6;
              Random rnd = new Random();
              while (length-- > 0)
              {
                  int index = (int)(rnd.NextDouble() * charPool.Length);
                  if (charPool[index] != "-")
                  {
                      rs.Append(charPool[index]);
                      charPool[index] = "-";
                  }
                  else
                      length++;
              }
              return rs.ToString();
          }
          catch (Exception ex)
          {
              //ErrorLog.WriteErrorLog("Barcode", ex.ToString(), ex.Message);
          }
          return "";
      }
      public Byte[] getBarcodeImage(string barcode, string file)
      {
          try
          {
              BarCode39 _barcode = new BarCode39();
              int barSize =25;
              string fontFile = HttpContext.Current.Server.MapPath("~/fonts/FREE3OF9.TTF");
              return (_barcode.Code39(barcode, barSize, true, file, fontFile));
          }
          catch (Exception ex)
          {

          }
          return null;
      }



BarCode39.cs-->
private const int _itemSepHeight = 3;
      SizeF _titleSize = SizeF.Empty;
      SizeF _barCodeSize = SizeF.Empty;
      SizeF _codeStringSize = SizeF.Empty;
      public BarCode39()
      {
          _titleFont = new Font("Arial", 10);
          _codeStringFont = new Font("Arial", 10);
      }
      #region Barcode Title
      private string _titleString = null;
      private Font _titleFont = null;
      public string Title
      {
          get { return _titleString; }
          set { _titleString = value; }
      }
      public Font TitleFont
      {
          get { return _titleFont; }
          set { _titleFont = value; }
      }
      #endregion
      #region Barcode code string
      private bool _showCodeString = false;
      private Font _codeStringFont = null;
      public bool ShowCodeString
      {
          get { return _showCodeString; }
          set { _showCodeString = value; }
      }
      public Font CodeStringFont
      {
          get { return _codeStringFont; }
          set { _codeStringFont = value; }
      }
      #endregion
      #region Barcode Font
      private Font _c39Font = null;
      private float _c39FontSize = 12;
      private string _c39FontFileName = null;
      private string _c39FontFamilyName = null;
      public string FontFileName
      {
          get { return _c39FontFileName; }
          set { _c39FontFileName = value; }
      }
      public string FontFamilyName
      {
          get { return _c39FontFamilyName; }
          set { _c39FontFamilyName = value; }
      }
      public float FontSize
      {
          get { return _c39FontSize; }
          set { _c39FontSize = value; }
      }
      private Font Code39Font
      {
          get
          {
              if (_c39Font == null)
              {
                  // Load the barcode font
                  PrivateFontCollection pfc = new PrivateFontCollection();
                  pfc.AddFontFile(_c39FontFileName);
                  FontFamily family = new FontFamily(_c39FontFamilyName, pfc);
                  _c39Font = new Font(family, _c39FontSize);
              }
              return _c39Font;
          }
      }
      #endregion
      #region Barcode Generation
      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));
      }

      #endregion
      #region Auxiliary Methods
      private int Max(int v1, int v2)
      {
          return (v1 > v2 ? v1 : v2);
      }
      private int XCentered(int localWidth, int globalWidth)
      {
          return ((globalWidth - localWidth) / 2);
      }
      #endregion
  }
Posted
Comments
Richard MacCutchan 28-Jan-17 4:34am    
Where is the scanner, and where is the code to read from it?
Member 12964836 13-Feb-17 7:52am    
I have a Scanner and Printer Also,But Problem is that Image is getting successfully created ,but I don't know how to print that image through Bar-code Printer.
Richard MacCutchan 13-Feb-17 8:00am    
You would need to read the documentation for the barcode printer.
Member 12964836 13-Feb-17 1:30am    
I have a Scanner and Printer Also,But Problem is that Image is getting successfully created ,but I don't know how to print that image through Bar-code Printer.

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

  Print Answers RSS


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