Barcode Generator
Idea shared by Administrator - January 3 at 12:42 PM
Proposed
Employee Post
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_Barcode" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;

<html xmlns="http://www.w3.org/1999/xhtml">;
<head runat="server">
    <title>Barcode Generator</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p align="center"><asp:Button ID="btnSubmit" Text="Generate Barcode" OnClick="btnSubmit_Click" runat="server" /></p>
        <p align="center"><asp:Image ID="imgFile" runat="server" /></p> 
    </div>
    </form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default_Barcode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string _sBarcode = "JOHN-DOE"; //- For Testing Use.
        int iSudentId = 1010;  //- Assume that the StudentId working is 1010
        string _SAMPLE_CREATED_BARCODE = "~/imported-items/" + iSudentId.ToString() + "/" + _sBarcode + ".jpg";
        //- Instantiate the Barcode Generator Object
        BarcodeHelper oObjx = new BarcodeHelper(_sBarcode, iSudentId);
        if (ncTools.DFO.TheFileExists(Server.MapPath( _SAMPLE_CREATED_BARCODE)))
        {
            imgFile.ImageUrl = _SAMPLE_CREATED_BARCODE;
        }
    }
}
BarcodeHelper.cs
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;
using System.Web.UI.Adapters;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


/// <summary>
/// Summary description for BarcodeHelper
/// </summary>
public class BarcodeHelper
{
    private int _iStudentId=0;
    public int StudentId
    {
        get {return _iStudentId;}
    }
    public BarcodeHelper(string BarCodeInput, int pStudentId)
	{
        if (BarCodeInput.Trim().Length > 0 && pStudentId>0)
        {
            _iStudentId = pStudentId;
            GenerateBarCode(BarCodeInput);
        }
	}
    private void GenerateBarCode(string BarCodeInput)
    {
        {
            BarcodeLib.Barcode b = new BarcodeLib.Barcode();
            System.Drawing.Image BarcodeImage = null;

            //-- errorProvider1.Clear();
            int W = Convert.ToInt32("300");
            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;
            //-(was)--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
                    //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);
                        //-(was)--ms.WriteTo(HttpContext.Current.Response.OutputStream);
                        Byte[] imgfi = ms.ToArray();
                        //- Save it.
                        WriteBarcodeImageToDisk(imgfi, BarCodeInput);
                    }

                }
                //-reposition the barcode image to the middle
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message.ToString());
            }
          }
        }
        private void WriteBarcodeImageToDisk(Byte[] pParamValue, string pBarcodeName)
        {            
            string FILE_NAME_FOLDER_TOBE_SAVED = "~/imported-items/" + StudentId.ToString() + "/" + pBarcodeName.Trim() + ".jpg";
            if (ncTools.DFO.DirectoryFolderExists("~/imported-items/" + StudentId.ToString()))
            {
                String st = HttpContext.Current.Server.MapPath(FILE_NAME_FOLDER_TOBE_SAVED);
                FileStream fs = new FileStream(st, FileMode.Create, FileAccess.Write);
                fs.Write(pParamValue, 0, pParamValue.Length);
                fs.Close();
            }
        }    
}

Reply to Thread