[ASP.NET MVC] 产生一维条码Barcode(Code 39、Code128、ISBN)

时间:2019-09-13
本文章向大家介绍[ASP.NET MVC] 产生一维条码Barcode(Code 39、Code128、ISBN),主要包括[ASP.NET MVC] 产生一维条码Barcode(Code 39、Code128、ISBN)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

[ASP.NET MVC]Barcode 产生一维条码(Code 39、Code128、ISBN)


最近项目刚好要产生Code39一维条码,找到了这个Library

BarcodeLib

支持多种(Code 128、Code 11、Code 39..等等)

(图节录自http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library)

说明:

1.先下载BarcodeLib

2.把BarcodeLib.dll加入参考至项目

3.建立Barcode方法在Controller

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Web.Mvc;
   6: using BarcodeLib;
   7: using System.Drawing;
   8: using System.Drawing.Imaging;
   9: namespace BarcodeDemo.Controllers
  10: {
  11:     public class HomeController : Controller
  12:     {
  13:         // GET: Home
  14:         public ActionResult Index()
  15:         {
  16:             return View();
  17:         }
  18:  
  19:         public void Barcode(string sn = null)
  20:         {
  21:             Response.ContentType = "image/gif";
  22:             Barcode bc = new Barcode();
  23:             bc.IncludeLabel = true;//显示文字标签
  24:             bc.LabelFont = new Font("Verdana", 9f);//文字标签字型、大小
  25:             bc.Width = 300;//宽度
  26:             bc.Height = 100;//高度
  27:             Image img = bc.Encode(TYPE.CODE39, sn, bc.Width, bc.Height);//产生影像
  28:             img.Save(Response.OutputStream, ImageFormat.Gif);
  29:             Response.End();
  30:         }
  31:     }
  32: }


PS:如需修改条码类型,则修改{TYPE.CODE39}部分

4.这时候我们已经可以用网址取得图片了

但如果想在网页中加入

在网页中加入此行,SN部分则为条码内容

   1: 


PS:笔者在编码内容为写死,通常会从数据库中抓取该商品的编码内容

另外可以依照需求把图片产生后存在server端,才不用每次读取时再算一次图。

附上此范例项目档(GitHub):https://github.com/mrsunboss/BarcodeDemo_WEB


如有错误还请各位先进前辈们不吝啬的指教,谢谢。

 

原文:大专栏  [ASP.NET MVC] 产生一维条码Barcode(Code 39、Code128、ISBN)


原文地址:https://www.cnblogs.com/chinatrump/p/11516491.html