Create and Save Bar Code in asp.net with C#

Create and Save Bar Code in asp.net with C#



Please download "IDAutomationHC39M" font online.

.Aspx Page :


<form id="form1" runat="server">
        <asp:TextBox ID=" txtInput " runat="server"></asp:TextBox>
        <asp:Button ID="btnGenerate" runat="server" Text="Generate" OnClick="btnGenerate_Click" />
        <hr />
        <asp:PlaceHolder ID="plBarCode" runat="server" />

    </form>


.Cs Page :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using OnBarcode.Barcode;
//using Zen.Barcode;

public partial class BarCode : System.Web.UI.Page
{
    protected void btnGenerate_Click(object sender, EventArgs e)
    {
       
        string barCode = txtInput.Text;
      

        System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
        using (Bitmap bitMap = new Bitmap(barCode.Length * 30, 60))
        {
            using (Graphics graphics = Graphics.FromImage(bitMap))
            {
                Font oFont = new Font("IDAutomationHC39M", 20);
                //Font oFont = new Font("Code 3 de 9", 50);
                //Font oFont = new Font("Code 128", 80);
                //Font oFont = new Font("Code 2 of 5 interleaved", 40);
                //Font oFont = new Font("barcode font", 70);
                PointF point = new PointF(2f, 2f);
                SolidBrush blackBrush = new SolidBrush(Color.Black);
                SolidBrush whiteBrush = new SolidBrush(Color.White);
                graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
                graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
            }
            using (MemoryStream ms = new MemoryStream())
            {
                bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] byteImage = ms.ToArray();

                string result = Convert.ToBase64String(byteImage);
                CreateImage(result.ToString());
                imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
            }
            plBarCode.Controls.Add(imgBarCode);
        }
    }

    public string CreateImage(string Byt)
    {
        try
        {
            byte[] data = Convert.FromBase64String(Byt);

            var filename = Convert.ToString(System.Guid.NewGuid()).Substring(0, 5) + Convert.ToString(System.Guid.NewGuid()).Substring(0, 5) + System.DateTime.Now.ToString("FFFFFF") + System.DateTime.Now.Minute + ".png";// +System.DateTime.Now.ToString("fffffffffff") + ".png";
            var file = HttpContext.Current.Server.MapPath("~/AppImages/" + filename);
            System.IO.File.WriteAllBytes(file, data);
            string ImgName = ".../profileimages/" + filename;

            return filename;
        }
        catch (Exception e)
        {
            return "Error";

        }
    }
}

It Will look like :





Comments

Post a Comment

Popular posts from this blog

Create and save QR code in asp.net with C#

Change text of RadGrid Header Dynamically

Telerik Radwindow Open on Button Click