Barcode Generator
Idea shared by Administrator - April 8, 2018 at 12:03 PM
Proposed
Employee Post
C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;

public void GenerateBarCode(string BarCodeInput)
        {
            {
                BarcodeLib.Barcode b = new BarcodeLib.Barcode();
                System.Drawing.Image BarcodeImage = null;

                //-- errorProvider1.Clear();
                //int W = Convert.ToInt32("240");
                //int H = Convert.ToInt32("25");
                int W = Convert.ToInt32("600");
                int H = Convert.ToInt32("64");
                b.Alignment = BarcodeLib.AlignmentPositions.CENTER;

                //--barcode alignment
                b.Alignment = BarcodeLib.AlignmentPositions.CENTER;

                BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED;
                //switch (cbEncodeType.SelectedItem.ToString().Trim())
                //{
                //    case "UPC-A": type = BarcodeLib.TYPE.UPCA; break;
                //    case "UPC-E": type = BarcodeLib.TYPE.UPCE; break;
                //    case "UPC 2 Digit Ext.": type = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_2DIGIT; break;
                //    case "UPC 5 Digit Ext.": type = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_5DIGIT; break;
                //    case "EAN-13": type = BarcodeLib.TYPE.EAN13; break;
                //    case "JAN-13": type = BarcodeLib.TYPE.JAN13; break;
                //    case "EAN-8": type = BarcodeLib.TYPE.EAN8; break;
                //    case "ITF-14": type = BarcodeLib.TYPE.ITF14; break;
                //    case "Codabar": type = BarcodeLib.TYPE.Codabar; break;
                //    case "PostNet": type = BarcodeLib.TYPE.PostNet; break;
                //    case "Bookland/ISBN": type = BarcodeLib.TYPE.BOOKLAND; break;
                //-type = BarcodeLib.TYPE.CODE11;
                type = BarcodeLib.TYPE.CODE39Extended;
                //    case "Code 39": type = BarcodeLib.TYPE.CODE39; break;
                //    case "Code 39 Extended": type = BarcodeLib.TYPE.CODE39Extended; break;
                //    case "Code 39 Mod 43": type = BarcodeLib.TYPE.CODE39_Mod43; break;
                //    case "Code 93": type = BarcodeLib.TYPE.CODE93; break;
                //    case "LOGMARS": type = BarcodeLib.TYPE.LOGMARS; break;
                //    case "MSI": type = BarcodeLib.TYPE.MSI_Mod10; break;
                //    case "Interleaved 2 of 5": type = BarcodeLib.TYPE.Interleaved2of5; break;
                //    case "Standard 2 of 5": type = BarcodeLib.TYPE.Standard2of5; break;
                //    case "Code 128": type = BarcodeLib.TYPE.CODE128; break;
                //    case "Code 128-A": type = BarcodeLib.TYPE.CODE128A; break;
                //    case "Code 128-B": type = BarcodeLib.TYPE.CODE128B; break;
                //    case "Code 128-C": type = BarcodeLib.TYPE.CODE128C; break;
                //    case "Telepen": type = BarcodeLib.TYPE.TELEPEN; break;
                //    case "FIM": type = BarcodeLib.TYPE.FIM; break;
                //    case "Pharmacode": type = BarcodeLib.TYPE.PHARMACODE; break;
                //    default: MessageBox.Show("Please specify the encoding type."); break;
                //}//switch

                try
                {
                    if (type != BarcodeLib.TYPE.UNSPECIFIED)
                    {
                        try
                        {
                            b.BarWidth = null;
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Unable to parse BarWidth: " + ex.Message, ex);
                        }
                        try
                        {
                            b.AspectRatio = null;
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Unable to parse AspectRatio: " + ex.Message, ex);
                        }

                        b.IncludeLabel = true; // this.chkGenerateLabel.Checked;
                        b.RotateFlipType = RotateFlipType.RotateNoneFlipNone; // (RotateFlipType)Enum.Parse(typeof(RotateFlipType), this.cbRotateFlip.SelectedItem.ToString(), true);

                        //label alignment and position  Barcode on top and Value underneath center
                        //switch (this.cbLabelLocation.SelectedItem.ToString().Trim().ToUpper())
                        //{
                        //    case "BOTTOMLEFT":  b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMLEFT; break;
                        //    case "BOTTOMRIGHT": b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMRIGHT; break;
                        //    case "TOPCENTER": b.LabelPosition = BarcodeLib.LabelPositions.TOPCENTER; break;
                        //    case "TOPLEFT": b.LabelPosition = BarcodeLib.LabelPositions.TOPLEFT; break;
                        //    case "TOPRIGHT": b.LabelPosition = BarcodeLib.LabelPositions.TOPRIGHT; break;
                        b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;                        
                        //}//switch

                        //===== Encoding performed here =====
                        BarcodeImage = b.Encode(type, BarCodeInput, Color.Black, Color.White, W, H);
                        //===================================
                        using (MemoryStream ms = new MemoryStream())
                        {
                            BarcodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                            //-ms.WriteTo(HttpContext.Current.Response.OutputStream);
                            //-Byte[] imgfi = Convert.ToByte(HttpContext.Current.Response.OutputStream);
                            Byte[] imgfi = ms.ToArray();
                            WriteImageToDisk(imgfi, BarCodeInput);
                        }

                    }
                    //reposition the barcode image to the middle
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message.ToString());
                }
            }
        }
        private void WriteImageToDisk(Byte[] pParamValue,string pBarcodeName)
        {
            //-Byte[] ar = Ret_image(Convert.ToInt32(pParamValue));
            String st = HttpContext.Current.Server.MapPath("~/export/" + pBarcodeName.Trim() + ".png");
            FileStream fs = new FileStream(st, FileMode.Create, FileAccess.Write);
            fs.Write(pParamValue, 0, pParamValue.Length);
            fs.Close();
            //-Image1.ImageUrl = "abc.jpg";        
        }        
        private byte[] ImageToBinary(string _path)
        {
            FileStream fS = new FileStream(_path, FileMode.Open, FileAccess.Read);
            byte[] b = new byte[fS.Length];
            fS.Read(b, 0, (int)fS.Length);
            fS.Close();
            return b;
        }
        private System.Drawing.Image BinaryToImage(byte[] b)
        {
            if (b == null)
                return null;

            MemoryStream memStream = new MemoryStream();
            memStream.Write(b, 0, b.Length);
            return System.Drawing.Image.FromStream(memStream);
        }
Download Library here-->​ BarcodeLib.zip

Reply to Thread