Ranish666

barcode print

Apr 1st, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. string font = ConfigurationManager.AppSettings["BarCodeFontFamily"].ToString();
  2. string barcode = obj[i].barcode;
  3.                 if (font != "Code 128")
  4.                 {
  5.                     using (Bitmap bitMap = new Bitmap(barcode.Length * 40, 100))
  6.                     {
  7.                         System.Drawing.Font oFont = new System.Drawing.Font(font, 18);
  8.  
  9.                         using (Graphics graphics = Graphics.FromImage(bitMap))
  10.                         {
  11.                             //System.Drawing.Font oFont = new System.Drawing.Font("IDAutomationHC39M", 18);
  12.                             //System.Drawing.Font oFont = new System.Drawing.Font("IDAHC39M Code 39 Barcode", 16);
  13.                             PointF point = new PointF(2f, 2f);
  14.                             SolidBrush whiteBrush = new SolidBrush(Color.White);
  15.                             graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
  16.                             SolidBrush blackBrush = new SolidBrush(Color.Black);
  17.                             graphics.DrawString("*" + barcode + "*", oFont, blackBrush, point);
  18.  
  19.                         }
  20.                         using (MemoryStream ms = new MemoryStream())
  21.                         {
  22.                             bitMap.Save(ms, ImageFormat.Png);
  23.                             byte[] byteImage = ms.ToArray();
  24.                             Convert.ToBase64String(byteImage);
  25.                             image = "data:image/png;base64," + Convert.ToBase64String(byteImage);
  26.                         }
  27.                     }
  28.                 }
  29.                 else
  30.                 {
  31.                     //var bitMap = GenCode128.Code128Rendering.MakeBarcodeImage(barcode, 1, false);
  32.                     Barcode128 code128 = new Barcode128();
  33.                     code128.CodeType = Barcode.CODE128;
  34.                     code128.ChecksumText = true;
  35.                     code128.GenerateChecksum = true;
  36.                     code128.Code = barcode;
  37.                     var bitMap = new System.Drawing.Bitmap(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White));
  38.                    
  39.                     using (MemoryStream ms = new MemoryStream())
  40.                     {
  41.                         bitMap.Save(ms, ImageFormat.Gif);
  42.                         byte[] byteImage = ms.ToArray();
  43.                         Convert.ToBase64String(byteImage);
  44.                         image = "data:image/Gif;base64," + Convert.ToBase64String(byteImage);
  45.                     }
  46.                 }
Add Comment
Please, Sign In to add comment