Generate Barcode in ASP.NET MVC - ASP.NET
BarCode SDK sample in ASP.NET demonstrating ‘Generate Barcode in ASP.NET MVC’
BytescoutBarCodeHelper.cs
using System;
using System.Web;
using System.Web.Mvc;
using System.Linq;
using Bytescout.BarCode;
namespace BarcodeGenerator.Helpers
{
public static class GeneratorHelper
{
public static string GetBase64Barcode(string value, SymbologyType symbology, bool addChecksum = false)
{
using (var barcode = new Barcode())
{
barcode.Symbology = symbology;
barcode.Value = value;
barcode.AddChecksum = addChecksum;
var imageBytes = barcode.GetImageBytesPNG();
return Convert.ToBase64String(imageBytes);
}
}
public static IHtmlString GetBarcode(this HtmlHelper helper,
string value,
SymbologyType symbology,
bool addChecksum = false)
{
var base64ImageBytes = GetBase64Barcode(value, symbology, addChecksum);
var str = string.Format("<img src=\"data:image/png;base64,{0}\">", base64ImageBytes);
return MvcHtmlString.Create(str);
}
public static IHtmlString EmbeddedBarcodeImage(this HtmlHelper helper,
string value,
SymbologyType symbology,
bool addChecksum = false)
{
using (var barcode = new Barcode())
{
barcode.Symbology = symbology;
barcode.Value = value;
barcode.AddChecksum = addChecksum;
var imageBytes = barcode.GetImageBytesPNG();
var str = string.Format("data:image/png;base64,{0}",
Convert.ToBase64String(imageBytes));
return MvcHtmlString.Create(str);
}
}
}
}
Global.asax.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace MvcApplication1
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
BundleConfig.cs
using System.Web;
using System.Web.Optimization;
namespace MvcApplication1
{
public class BundleConfig
{
// For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery-ui-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
"~/Content/themes/base/jquery.ui.core.css",
"~/Content/themes/base/jquery.ui.resizable.css",
"~/Content/themes/base/jquery.ui.selectable.css",
"~/Content/themes/base/jquery.ui.accordion.css",
"~/Content/themes/base/jquery.ui.autocomplete.css",
"~/Content/themes/base/jquery.ui.button.css",
"~/Content/themes/base/jquery.ui.dialog.css",
"~/Content/themes/base/jquery.ui.slider.css",
"~/Content/themes/base/jquery.ui.tabs.css",
"~/Content/themes/base/jquery.ui.datepicker.css",
"~/Content/themes/base/jquery.ui.progressbar.css",
"~/Content/themes/base/jquery.ui.theme.css"));
}
}
}
FilterConfig.cs
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
}
RouteConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcApplication1
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
WebApiConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace MvcApplication1
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bytescout.BarCode; // add reference from c:\program files\bytescout barcode generator sdk\net4.00\
using BarcodeGenerator.Helpers; // reference to Helpers/BytescoutBarCodeHelper.cs
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
/*
IF YOU SEE TEMPORARY FOLDER ACCESS ERRORS:
Temporary folder access is required for web application when you use ByteScout SDK in it.
If you are getting errors related to the access to temporary folder like "Access to the path 'C:\Windows\TEMP\... is denied" then you need to add permission for this temporary folder to make ByteScout SDK working on that machine and IIS configuration because ByteScout SDK requires access to temp folder to cache some of its data for more efficient work.
SOLUTION:
If your IIS Application Pool has "Load User Profile" option enabled the IIS provides access to user's temp folder. Check user's temporary folder
If you are running Web Application under an impersonated account or IIS_IUSRS group, IIS may redirect all requests into separate temp folder like "c:\temp\".
In this case
- check the User or User Group your web application is running under
- then add permissions for this User or User Group to read and write into that temp folder (c:\temp or c:\windows\temp\ folder)
- restart your web application and try again
*/
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string theValue)
{
var base64ImageBytes = "";
try
{
bool addChecksum = false;
// generate QR Code barcode
base64ImageBytes = GeneratorHelper.GetBase64Barcode(theValue, SymbologyType.QRCode, addChecksum);
}
catch (Exception e)
{
ViewBag.Error = e.Message;
return View();
}
var str = string.Format("<img src=\"data:image/png;base64,{0}\">", base64ImageBytes);
ViewBag.Image = str;
return View();
}
}
}
BytescoutBarCodeHelper.cs
using System;
using System.Web;
using System.Web.Mvc;
using System.Linq;
using Bytescout.BarCode; // add reference from c:\program files\bytescout barcode generator sdk\net4.00\
namespace BarcodeGenerator.Helpers
{
public static class GeneratorHelper
{
/// <summary>
/// Generate barcode and return as base 64 image
/// </summary>
/// <param name="value">value</param>
/// <param name="symbology">barcode type</param>
/// <param name="addChecksum">add checkum or not</param>
/// <returns></returns>
public static string GetBase64Barcode(string value, SymbologyType symbology, bool addChecksum = false)
{
using (var barcode = new Barcode())
{
barcode.Symbology = symbology;
barcode.Value = value;
barcode.AddChecksum = addChecksum;
var imageBytes = barcode.GetImageBytesPNG();
return Convert.ToBase64String(imageBytes);
}
}
/// <summary>
/// Returns HTML string with the image with barcode
/// </summary>
/// <param name="helper"></param>
/// <param name="value"></param>
/// <param name="symbology"></param>
/// <param name="addChecksum"></param>
/// <returns></returns>
public static IHtmlString GetBarcode(this HtmlHelper helper,
string value,
SymbologyType symbology,
bool addChecksum = false)
{
var base64ImageBytes = GetBase64Barcode(value, symbology, addChecksum);
var str = string.Format("<img src=\"data:image/png;base64,{0}\">", base64ImageBytes);
return MvcHtmlString.Create(str);
}
/// <summary>
/// Returns embedded HTML barcode image
/// </summary>
/// <param name="helper"></param>
/// <param name="value"></param>
/// <param name="symbology"></param>
/// <param name="addChecksum"></param>
/// <returns></returns>
public static IHtmlString EmbeddedBarcodeImage(this HtmlHelper helper,
string value,
SymbologyType symbology,
bool addChecksum = false)
{
using (var barcode = new Barcode())
{
barcode.Symbology = symbology;
barcode.Value = value;
barcode.AddChecksum = addChecksum;
var imageBytes = barcode.GetImageBytesPNG();
var str = string.Format("data:image/png;base64,{0}",
Convert.ToBase64String(imageBytes));
return MvcHtmlString.Create(str);
}
}
}
}
_references.js
��/ / / <