It is my first article about to generate bar code on MVC through IDAutomation_Code39FreeFont.
This article explain you how to generate bar code step by step very easily.
Following step follow to generate bar code:
- Install IDAutomation_Code39FreeFont on the system which I attached to my article.
- Go to control panel -> Font -> and check the IDAutomation_CodeHC39M Free Version (font name) is installed or not ones its installed go to further steps.
- File -> New -> Project -> ASP.NET MVC 4 Web Application ->Name: BarCodeGenerators.
- In the Controllers folder Just Right Click and Add Controller name as BarCodeController.cs.
- Write ActionResult name as BarCode.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace BarCodeGenerators.Controllers
- {
- public class BarCodeController: Controller
- {
-
- public ActionResult BarCode()
- {
- return View();
- }
- }
- }
- Just right click on ActionResult BarCode and add veiw.
- Add class file name as IDAtomationBarCode.cs in the views folder.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Drawing;
- using System.Text;
- using System.IO;
- using System.Drawing.Imaging;
- namespace BarCodeGenerators.Views.BarCode
- {
- public class IDAtomationBarCode
- {
- public static string BarcodeImageGenerator(string Code)
- {
- byte[] BarCode;
- string BarCodeImage;
- Bitmap objBitmap = new Bitmap(Code.Length * 28, 100);
- using(Graphics graphic = Graphics.FromImage(objBitmap))
- {
- Font newFont = new Font("IDAutomationHC39M Free Version", 18, FontStyle.Regular);
- PointF point = new PointF(2 f, 2 f);
- SolidBrush balck = new SolidBrush(Color.Black);
- SolidBrush white = new SolidBrush(Color.White);
- graphic.FillRectangle(white, 0, 0, objBitmap.Width, objBitmap.Height);
- graphic.DrawString("*" + Code + "*", newFont, balck, point);
- }
- using(MemoryStream Mmst = new MemoryStream())
- {
- objBitmap.Save(Mmst, ImageFormat.Png);
- BarCode = Mmst.GetBuffer();
- BarCodeImage = BarCode != null ? "data:image/jpg;base64," + Convert.ToBase64String((byte[]) BarCode) : "";
- return BarCodeImage;
- }
- }
- }
- }
Note: BarcodeImageGenerator method will generate barcode you can call this method in view and get genrated barcode show on image tag.
- Go to the view code and write.
- @using BarCodeGenerators.Views.BarCode;
- @{
- ViewBag.Title = "BarCode";
- }
- < h2 > BarCode < /h2>
- @{
- var a = IDAtomationBarCode.BarcodeImageGenerator("12345678"); < img src = "@Url.Content(a)"
- alt = "Alternate Text"
- width = "400"
- height = "100" / >
- }
You can pass any value for generating barcode in BarcodeImageGenerator methode.