Monday, March 5, 2012

Barcode Sample Code

<head>
<title>Barcode Generator 1D for ASP.net</title>
    <style type="text/css">
    *
    {
        font-family: Verdana;
        font-size: 10pt;
    }
   
    #table_barcode
    {
        width: 500px;
        border: 1px solid #841414;
        border-collapse: collapse;
    }

    #table_barcode td
    {
        text-align: left;
        border: 1px solid #a73e3e;
        padding: 4px;
    }
   
    .table_header
    {
        background-color: #993535;
        color: White;
        text-decoration: underline;
    }
   
    .table_section
    {
        background-color: #c35d5d;
        color: white;
    }
   
    .row1
    {
        background-color: #f8d3d3;
    }
   
    .row2
    {
        background-color: #e1a6a6;
    }
   
    .empty
    {
        border-collapse: collapse;
    }
   
    .empty td
    {
        padding: 0px !important;
        border: 0px !important;
    }
   
    .footer, .footer a, .footer span
    {
        font-size: 10px;
    }
    </style>
</head>
<form id="form1" runat="server">
    <asp:ScriptManager runat="server" ID="scriptManager"></asp:ScriptManager>
   
    <div style="text-align: center;">

    <table id="table_barcode" style="margin: auto;">
        <tr><td colspan="2" style="text-align: center;" class="table_section">Bar Code</td></tr>
        <tr class="row2"><td>Text</td><td><asp:TextBox runat="server" ID="general_Text"></asp:TextBox></td></tr>
        <tr class="row1"><td colspan="2" style="text-align: center;"><asp:Button runat="server" ID="generate" Text="Generate" OnClick="GenerateClick" /></td></tr>
        <tr runat="server" id="imagerow"><td colspan="2" style="text-align: center;"><asp:Image runat="server" ID="barcodeimage" AlternateText="Barcode Image"></asp:Image></td></tr>
        </table>

    <p class="footer">
    All Rights Reserved &copy; 2009 <a href="http://www.barcodeasp.com" target="_blank">Barcode Generator</a> ASP.net-v<asp:Label runat="server" ID="versionLabel" />
    <br />by Jean-Sébastien Goupil
    </p>

    </div>
    </form>

Barcode.aspx.cs:

   public void GenerateClick(object sender, EventArgs e)
        {
            Random r = new Random();

            int a = Convert.ToInt32(general_Text.Text);

            string text = a.ToString();

            barcode temporaryBarcode = FindSelectedBarcode();

            if (!string.IsNullOrEmpty(text))
            {
                imagerow.Visible = true;
                barcodeimage.ImageUrl = string.Format("image.aspx?code={0}",text);
            }
            else
            {
                imagerow.Visible = false;
            }
        }

       
        private barcode FindSelectedBarcode()
        {
            Type codeType = null;
            foreach (KeyValuePair<Type, string> kvp in Utility.codeType)
            {
                if (kvp.Value == "Code 39")
                {
                    codeType = kvp.Key;
                    break;
                }
            }

            return (barcode)Activator.CreateInstance(codeType);
        }
Image.aspx Page Load Code:
try
            {
               
                BCGFont font = null;
               
                float fontSize = 8.0F;
               
                font = new BCGFont(new Font("Arial", fontSize));
              

                BCGColor color_black = new BCGColor(Color.Black);
                BCGColor color_white = new BCGColor(Color.White);

                BCGBarcode1D codebar = null;

                string code = "Code 39";
                Type codeType = null;
                foreach (KeyValuePair<Type, string> kvp in Utility.codeType)
                {
                    if (kvp.Value == code)
                    {
                        codeType = kvp.Key;
                        break;
                    }
                }

                if (codeType == null)
                {
                    throw new Exception("You need to specify a type of barcode");
                }

                barcode temporaryBarcode = (barcode)Activator.CreateInstance(codeType);
                codebar = (BCGBarcode1D)Activator.CreateInstance(temporaryBarcode.Code);

                int thickness = 30;
               
                codebar.setThickness(thickness);

                int scale = 1;
               
                codebar.setScale(scale);

               

                codebar.setBackgroundColor(color_white);
                codebar.setForegroundColor(color_black);
                codebar.setFont(font);
                codebar.parse(Request.QueryString["code"]);

                BCGDrawing drawing = new BCGDrawing(color_white);
                drawing.setBarcode(codebar);
                drawing.draw();

                //// Draw (or save) the image into the right format.
               
                ImageFormat imageFormat = null;
                imageFormat = ImageFormat.Png;
               

                if (imageFormat != null)
                {
                    drawing.finish(imageFormat, Response.OutputStream);
                }
            }
            catch (Exception exception)
            {
                Response.ContentType = "image/png";

                using (FileStream fs = new FileStream("error.png", FileMode.Open))
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        byte[] buffer = new byte[fs.Length];
                        br.Read(buffer, 0, (int)fs.Length);

                        using (MemoryStream ms = new MemoryStream(buffer))
                        {
                            ms.WriteTo(Response.OutputStream);
                        }
                    }
                }

                // Here, check for the exception.
                System.Diagnostics.Debug.Write(exception);
               
            }

Utility.cs:
public static Dictionary<Type, string> codeType = new Dictionary<Type, string>();

        public static Dictionary<int, string> outputType = new Dictionary<int, string>();

        public static List<string> fontType = new List<string>();

       

        static Utility()
        {
           
            codeType.Add(typeof(code39), "Code 39");
           

        }
Code Folder u add these two class files:

barcode.cs:
public abstract class barcode
    {
        public abstract string Title
        {
            get;
        }

        public abstract string Description
        {
            get;
        }

        public abstract Type Code
        {
            get;
        }

       

        public abstract string Explanation
        {
            get;
        }

        public virtual int SizeKeys
        {
            get
            {
                return 25;
            }
        }

        public virtual string SpecificText
        {
            get
            {
                return "Specific Config";
            }
        }

       
    }
code39.cs:

public override Type Code
        {
            get
            {
                return typeof(BarcodeGenerator.BCGcode39);
            }
        }

        public override string Description
        {
            get
            {
                return "";
            }
        }

        public override string Title
        {
            get
            {
                return "Code 39";
            }
        }

        public override string Explanation
        {
            get
            {
                return "<ul style=\"margin: 0px; padding-left: 25px;\"><li>Known also as USS Code 39, 3 of 9.</li><li>Code 39 can encode alphanumeric characters.</li><li>The symbology is used in non-retail environment.</li><li>Code 39 is designed to encode 26 upper case letters, 10 digits and 7 special characters.</li><li>Code 39 has a checksum but it's rarely used.</li></ul>";
            }
        }

       

        public override string SpecificText
        {
            get
            {
                return "Checksum";
            }
        }


DLLS:
--BarCodeSample.dll
BarcodeGenerator.Common.dll
BarcodeGenerator.1D.dll


No comments:

Post a Comment