1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Drawing;
  6. using System.Data;
  7.  
  8. namespace MvcGuestBook.Common
  9. {
  10. //条形码公共类
  11. public class BarCode128
  12. {
  13. private DataTable m_Code128 = new DataTable();
  14. private uint m_Height = ;
  15. /// <summary>
  16. /// 高度
  17. /// </summary>
  18. public uint Height { get { return m_Height; } set { m_Height = value; } }
  19. private Font m_ValueFont = null;
  20. /// <summary>
  21. /// 是否显示可见号码 如果为NULL不显示号码
  22. /// </summary>
  23. public Font ValueFont { get { return m_ValueFont; } set { m_ValueFont = value; } }
  24. private byte m_Magnify = ;
  25. /// <summary>
  26. /// 放大倍数
  27. /// </summary>
  28. public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } }
  29. /// <summary>
  30. /// 条码类别
  31. /// </summary>
  32. public enum Encode
  33. {
  34. Code128A,
  35. Code128B,
  36. Code128C,
  37. EAN128
  38. }
  39. public BarCode128()
  40. {
  41. m_Code128.Columns.Add("ID");
  42. m_Code128.Columns.Add("Code128A");
  43. m_Code128.Columns.Add("Code128B");
  44. m_Code128.Columns.Add("Code128C");
  45. m_Code128.Columns.Add("BandCode");
  46. m_Code128.CaseSensitive = true;
  47. #region 数据表
  48. m_Code128.Rows.Add("", " ", " ", "", "");
  49. m_Code128.Rows.Add("", "!", "!", "", "");
  50. m_Code128.Rows.Add("", "\"", "\"", "", "");
  51. m_Code128.Rows.Add("", "#", "#", "", "");
  52. m_Code128.Rows.Add("", "$", "$", "", "");
  53. m_Code128.Rows.Add("", "%", "%", "", "");
  54. m_Code128.Rows.Add("", "&", "&", "", "");
  55. m_Code128.Rows.Add("", "'", "'", "", "");
  56. m_Code128.Rows.Add("", "(", "(", "", "");
  57. m_Code128.Rows.Add("", ")", ")", "", "");
  58. m_Code128.Rows.Add("", "*", "*", "", "");
  59. m_Code128.Rows.Add("", "+", "+", "", "");
  60. m_Code128.Rows.Add("", ",", ",", "", "");
  61. m_Code128.Rows.Add("", "-", "-", "", "");
  62. m_Code128.Rows.Add("", ".", ".", "", "");
  63. m_Code128.Rows.Add("", "/", "/", "", "");
  64. m_Code128.Rows.Add("", "", "", "", "");
  65. m_Code128.Rows.Add("", "", "", "", "");
  66. m_Code128.Rows.Add("", "", "", "", "");
  67. m_Code128.Rows.Add("", "", "", "", "");
  68. m_Code128.Rows.Add("", "", "", "", "");
  69. m_Code128.Rows.Add("", "", "", "", "");
  70. m_Code128.Rows.Add("", "", "", "", "");
  71. m_Code128.Rows.Add("", "", "", "", "");
  72. m_Code128.Rows.Add("", "", "", "", "");
  73. m_Code128.Rows.Add("", "", "", "", "");
  74. m_Code128.Rows.Add("", ":", ":", "", "");
  75. m_Code128.Rows.Add("", ";", ";", "", "");
  76. m_Code128.Rows.Add("", "<", "<", "", "");
  77. m_Code128.Rows.Add("", "=", "=", "", "");
  78. m_Code128.Rows.Add("", ">", ">", "", "");
  79. m_Code128.Rows.Add("", "?", "?", "", "");
  80. m_Code128.Rows.Add("", "@", "@", "", "");
  81. m_Code128.Rows.Add("", "A", "A", "", "");
  82. m_Code128.Rows.Add("", "B", "B", "", "");
  83. m_Code128.Rows.Add("", "C", "C", "", "");
  84. m_Code128.Rows.Add("", "D", "D", "", "");
  85. m_Code128.Rows.Add("", "E", "E", "", "");
  86. m_Code128.Rows.Add("", "F", "F", "", "");
  87. m_Code128.Rows.Add("", "G", "G", "", "");
  88. m_Code128.Rows.Add("", "H", "H", "", "");
  89. m_Code128.Rows.Add("", "I", "I", "", "");
  90. m_Code128.Rows.Add("", "J", "J", "", "");
  91. m_Code128.Rows.Add("", "K", "K", "", "");
  92. m_Code128.Rows.Add("", "L", "L", "", "");
  93. m_Code128.Rows.Add("", "M", "M", "", "");
  94. m_Code128.Rows.Add("", "N", "N", "", "");
  95. m_Code128.Rows.Add("", "O", "O", "", "");
  96. m_Code128.Rows.Add("", "P", "P", "", "");
  97. m_Code128.Rows.Add("", "Q", "Q", "", "");
  98. m_Code128.Rows.Add("", "R", "R", "", "");
  99. m_Code128.Rows.Add("", "S", "S", "", "");
  100. m_Code128.Rows.Add("", "T", "T", "", "");
  101. m_Code128.Rows.Add("", "U", "U", "", "");
  102. m_Code128.Rows.Add("", "V", "V", "", "");
  103. m_Code128.Rows.Add("", "W", "W", "", "");
  104. m_Code128.Rows.Add("", "X", "X", "", "");
  105. m_Code128.Rows.Add("", "Y", "Y", "", "");
  106. m_Code128.Rows.Add("", "Z", "Z", "", "");
  107. m_Code128.Rows.Add("", "[", "[", "", "");
  108. m_Code128.Rows.Add("", "\\", "\\", "", "");
  109. m_Code128.Rows.Add("", "]", "]", "", "");
  110. m_Code128.Rows.Add("", "^", "^", "", "");
  111. m_Code128.Rows.Add("", "_", "_", "", "");
  112. m_Code128.Rows.Add("", "NUL", "`", "", "");
  113. m_Code128.Rows.Add("", "SOH", "a", "", "");
  114. m_Code128.Rows.Add("", "STX", "b", "", "");
  115. m_Code128.Rows.Add("", "ETX", "c", "", "");
  116. m_Code128.Rows.Add("", "EOT", "d", "", "");
  117. m_Code128.Rows.Add("", "ENQ", "e", "", "");
  118. m_Code128.Rows.Add("", "ACK", "f", "", "");
  119. m_Code128.Rows.Add("", "BEL", "g", "", "");
  120. m_Code128.Rows.Add("", "BS", "h", "", "");
  121. m_Code128.Rows.Add("", "HT", "i", "", "");
  122. m_Code128.Rows.Add("", "LF", "j", "", "");
  123. m_Code128.Rows.Add("", "VT", "k", "", "");
  124. m_Code128.Rows.Add("", "FF", "I", "", "");
  125. m_Code128.Rows.Add("", "CR", "m", "", "");
  126. m_Code128.Rows.Add("", "SO", "n", "", "");
  127. m_Code128.Rows.Add("", "SI", "o", "", "");
  128. m_Code128.Rows.Add("", "DLE", "p", "", "");
  129. m_Code128.Rows.Add("", "DC1", "q", "", "");
  130. m_Code128.Rows.Add("", "DC2", "r", "", "");
  131. m_Code128.Rows.Add("", "DC3", "s", "", "");
  132. m_Code128.Rows.Add("", "DC4", "t", "", "");
  133. m_Code128.Rows.Add("", "NAK", "u", "", "");
  134. m_Code128.Rows.Add("", "SYN", "v", "", "");
  135. m_Code128.Rows.Add("", "ETB", "w", "", "");
  136. m_Code128.Rows.Add("", "CAN", "x", "", "");
  137. m_Code128.Rows.Add("", "EM", "y", "", "");
  138. m_Code128.Rows.Add("", "SUB", "z", "", "");
  139. m_Code128.Rows.Add("", "ESC", "{", "", "");
  140. m_Code128.Rows.Add("", "FS", "|", "", "");
  141. m_Code128.Rows.Add("", "GS", "}", "", "");
  142. m_Code128.Rows.Add("", "RS", "~", "", "");
  143. m_Code128.Rows.Add("", "US", "DEL", "", "");
  144. m_Code128.Rows.Add("", "FNC3", "FNC3", "", "");
  145. m_Code128.Rows.Add("", "FNC2", "FNC2", "", "");
  146. m_Code128.Rows.Add("", "SHIFT", "SHIFT", "", "");
  147. m_Code128.Rows.Add("", "CODEC", "CODEC", "", "");
  148. m_Code128.Rows.Add("", "CODEB", "FNC4", "CODEB", "");
  149. m_Code128.Rows.Add("", "FNC4", "CODEA", "CODEA", "");
  150. m_Code128.Rows.Add("", "FNC1", "FNC1", "FNC1", "");
  151. m_Code128.Rows.Add("", "StartA", "StartA", "StartA", "");
  152. m_Code128.Rows.Add("", "StartB", "StartB", "StartB", "");
  153. m_Code128.Rows.Add("", "StartC", "StartC", "StartC", "");
  154. m_Code128.Rows.Add("", "Stop", "Stop", "Stop", "");
  155. #endregion
  156. }
  157. /// <summary>
  158. /// 获取128图形
  159. /// </summary>
  160. /// <param name="p_Text">文字</param>
  161. /// <param name="p_Code">编码</param>
  162. /// <returns>图形</returns>
  163. public Bitmap GetCodeImage(string p_Text, Encode p_Code)
  164. {
  165. string _ViewText = p_Text;
  166. string _Text = "";
  167. IList<int> _TextNumb = new List<int>();
  168. int _Examine = ; //首位
  169. switch (p_Code)
  170. {
  171. case Encode.Code128C:
  172. _Examine = ;
  173. if (!((p_Text.Length & ) == )) throw new Exception("128C长度必须是偶数");
  174. while (p_Text.Length != )
  175. {
  176. int _Temp = ;
  177. try
  178. {
  179. int _CodeNumb128 = Int32.Parse(p_Text.Substring(, ));
  180. }
  181. catch
  182. {
  183. throw new Exception("128C必须是数字!");
  184. }
  185. _Text += GetValue(p_Code, p_Text.Substring(, ), ref _Temp);
  186. _TextNumb.Add(_Temp);
  187. p_Text = p_Text.Remove(, );
  188. }
  189. break;
  190. case Encode.EAN128:
  191. _Examine = ;
  192. if (!((p_Text.Length & ) == )) throw new Exception("EAN128长度必须是偶数");
  193. _TextNumb.Add();
  194. _Text += "";
  195. while (p_Text.Length != )
  196. {
  197. int _Temp = ;
  198. try
  199. {
  200. int _CodeNumb128 = Int32.Parse(p_Text.Substring(, ));
  201. }
  202. catch
  203. {
  204. throw new Exception("128C必须是数字!");
  205. }
  206. _Text += GetValue(Encode.Code128C, p_Text.Substring(, ), ref _Temp);
  207. _TextNumb.Add(_Temp);
  208. p_Text = p_Text.Remove(, );
  209. }
  210. break;
  211. default:
  212. if (p_Code == Encode.Code128A)
  213. {
  214. _Examine = ;
  215. }
  216. else
  217. {
  218. _Examine = ;
  219. }
  220.  
  221. while (p_Text.Length != )
  222. {
  223. int _Temp = ;
  224. string _ValueCode = GetValue(p_Code, p_Text.Substring(, ), ref _Temp);
  225. if (_ValueCode.Length == ) throw new Exception("无效的字符集!" + p_Text.Substring(, ).ToString());
  226. _Text += _ValueCode;
  227. _TextNumb.Add(_Temp);
  228. p_Text = p_Text.Remove(, );
  229. }
  230. break;
  231. }
  232. if (_TextNumb.Count == ) throw new Exception("错误的编码,无数据");
  233. _Text = _Text.Insert(, GetValue(_Examine)); //获取开始位
  234.  
  235. for (int i = ; i != _TextNumb.Count; i++)
  236. {
  237. _Examine += _TextNumb[i] * (i + );
  238. }
  239. _Examine = _Examine % ; //获得严效位
  240. _Text += GetValue(_Examine); //获取严效位
  241. _Text += ""; //结束位
  242. Bitmap _CodeImage = GetImage(_Text);
  243. GetViewText(_CodeImage, _ViewText);
  244. return _CodeImage;
  245. }
  246. /// <summary>
  247. /// 获取目标对应的数据
  248. /// </summary>
  249. /// <param name="p_Code">编码</param>
  250. /// <param name="p_Value">数值 A b 30</param>
  251. /// <param name="p_SetID">返回编号</param>
  252. /// <returns>编码</returns>
  253. private string GetValue(Encode p_Code, string p_Value, ref int p_SetID)
  254. {
  255. if (m_Code128 == null) return "";
  256. DataRow[] _Row = m_Code128.Select(p_Code.ToString() + "='" + p_Value + "'");
  257. if (_Row.Length != ) throw new Exception("错误的编码" + p_Value.ToString());
  258. p_SetID = Int32.Parse(_Row[]["ID"].ToString());
  259. return _Row[]["BandCode"].ToString();
  260. }
  261. /// <summary>
  262. /// 根据编号获得条纹
  263. /// </summary>
  264. /// <param name="p_CodeId"></param>
  265. /// <returns></returns>
  266. private string GetValue(int p_CodeId)
  267. {
  268. DataRow[] _Row = m_Code128.Select("ID='" + p_CodeId.ToString() + "'");
  269. if (_Row.Length != ) throw new Exception("验效位的编码错误" + p_CodeId.ToString());
  270. return _Row[]["BandCode"].ToString();
  271. }
  272. /// <summary>
  273. /// 获得条码图形
  274. /// </summary>
  275. /// <param name="p_Text">文字</param>
  276. /// <returns>图形</returns>
  277. private Bitmap GetImage(string p_Text)
  278. {
  279. char[] _Value = p_Text.ToCharArray();
  280. int _Width = ;
  281. for (int i = ; i != _Value.Length; i++)
  282. {
  283. _Width += Int32.Parse(_Value[i].ToString()) * (m_Magnify + );
  284. }
  285. Bitmap _CodeImage = new Bitmap(_Width, (int)m_Height);
  286. Graphics _Garphics = Graphics.FromImage(_CodeImage);
  287. //Pen _Pen;
  288. int _LenEx = ;
  289. for (int i = ; i != _Value.Length; i++)
  290. {
  291. int _ValueNumb = Int32.Parse(_Value[i].ToString()) * (m_Magnify + ); //获取宽和放大系数
  292. if (!((i & ) == ))
  293. {
  294. //_Pen = new Pen(Brushes.White, _ValueNumb);
  295. _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, , _ValueNumb, (int)m_Height));
  296. }
  297. else
  298. {
  299. //_Pen = new Pen(Brushes.Black, _ValueNumb);
  300. _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, , _ValueNumb, (int)m_Height));
  301. }
  302. //_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
  303. _LenEx += _ValueNumb;
  304. }
  305. _Garphics.Dispose();
  306. return _CodeImage;
  307. }
  308. /// <summary>
  309. /// 显示可见条码文字 如果小于40 不显示文字
  310. /// </summary>
  311. /// <param name="p_Bitmap">图形</param>
  312. private void GetViewText(Bitmap p_Bitmap, string p_ViewText)
  313. {
  314. if (m_ValueFont == null) return;
  315.  
  316. Graphics _Graphics = Graphics.FromImage(p_Bitmap);
  317. SizeF _DrawSize = _Graphics.MeasureString(p_ViewText, m_ValueFont);
  318. if (_DrawSize.Height > p_Bitmap.Height - || _DrawSize.Width > p_Bitmap.Width)
  319. {
  320. _Graphics.Dispose();
  321. return;
  322. }
  323.  
  324. int _StarX = p_Bitmap.Width - (int)_DrawSize.Width;
  325. int _StarY = p_Bitmap.Height - (int)_DrawSize.Height;
  326. _Graphics.FillRectangle(Brushes.White, new Rectangle(, _StarY, p_Bitmap.Width, (int)_DrawSize.Height));
  327. _Graphics.DrawString(p_ViewText, m_ValueFont, Brushes.Black, _StarX / , _StarY);//文字在图片的中间位置
  328. }
  329.  
  330. //12345678
  331. //(105 + (1 * 12 + 2 * 34 + 3 * 56 + 4 *78)) % 103 = 47
  332. //结果为starc +12 +34 +56 +78 +47 +end
  333.  
  334. internal Image GetCodeImage(string p)
  335. {
  336. throw new NotImplementedException();
  337. }
  338. }
  339. }
  1. //调用条形码方法
  2. public ActionResult BarCode()
  3. {
  4. BarCode128 code = new BarCode128();
  5. code.ValueFont = new System.Drawing.Font("Arial", );//声明条码下方的字体
  6. Bitmap bitmap = code.GetCodeImage("", BarCode128.Encode.Code128A);//通过对文本框的文件进行Code128编码获得位图
  7. string WebPath = "FlexPaper/BarCodeFile/" + DateTime.Now.ToString("yyyy-MM-dd");
  8. string filepath = Server.MapPath(WebPath);
  9. if (!Directory.Exists(filepath))
  10. Directory.CreateDirectory(filepath);
  11. string FileName = DateTime.Now.ToString("HHmmss") + ".jpg";
  12. bitmap.Save(filepath + "/" + FileName, ImageFormat.Jpeg);
  13. TempData["BarCode11"] = WebPath + "/" + FileName;
  14. return RedirectToAction("Index");
  15. }
  1. <% if (TempData["BarCode11"] != null)
  2. { %>
  3.  
  4. <img src="/Message/<%: TempData["BarCode11"] %>" alt="BarCode" />
  5. <%} %>
  6.  
  7. <%: Html.ActionLink("生成条形码", "BarCode")%>

基于ASP.NET生成条形码(code128)的更多相关文章

  1. 使用BarcodeLib.Barcode.ASP.NET生成条形码

    生成条形码图片,然后在前台页面展示: <img id="img" src="Mobile/<%=url %>"/> public str ...

  2. C#生成条形码 Code128算法

    条形有很多种,Code128是比较常用的一种,是一种高密度条码, CODE128 码可表示从 ASCII 0 到ASCII 127 共128个字符,故称128码.其中包含了数字.字母和符号字符. Co ...

  3. 基于ASP.NET生成二维码详细源码

    详细链接:https://shop499704308.taobao.com/?spm=a1z38n.10677092.card.11.594c1debsAGeakusing System; using ...

  4. asp.net 生成、解析条形码和二维码

    原文 asp.net 生成.解析条形码和二维码 一.条形码 一维码,俗称条形码,广泛的用于电子工业等行业.比如我们常见的书籍背面就会有条形码,通过扫描枪等设备扫描就可以获得书籍的ISBN(Intern ...

  5. 基于Asp.Net Core,利用ZXing来生成二维码的一般流程

    本文主要介绍如何在.net环境下,基于Asp.Net Core,利用ZXing来生成二维码的一般操作.对二维码工作原理了解,详情见:https://blog.csdn.net/weixin_36191 ...

  6. C# 在Word文档中生成条形码

    C# 在Word文档中生成条形码 简介 条形码是由多个不同的空白和黑条按照一定的顺序组成,用于表示各种信息如产品名称.制造商.类别.价格等.目前,条形码在我们的日常生活中有着很广泛的应用,不管是在图书 ...

  7. 使用html2canvas实现批量生成条形码

    /*前台代码*/ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Generat ...

  8. JAVA生成条形码

    1.下载生成条形码所需要的jar包barcode4j.jar: 2.java生成条形码代码 import java.awt.image.BufferedImage;import java.io.Fil ...

  9. C# 生成条形码

    原文地址:http://www.cnblogs.com/xcsn/p/4514759.html 引用BarcodeLib.dll(百度云中有)生成条形 protected void Button2_C ...

随机推荐

  1. springboot 2.0 自动装配原理 以redis为例

    当面试管问你springboot 和 普通spring 有什么区别? 您还在回答: 简化了配置 ,内置tomcat 等等 吗 ? 那只是皮毛, 最重要的还是自动化配置.一起来了解一下 第一步: 第二步 ...

  2. 20165233 学习基础和C语言基础调查

    学习基础与<做中学>阅读心得 读<做中学>有感 娄老师通过在学习工作中总结出的"做中学"的方式,将其运用到减肥.五笔训练.乒乓球训练以及英文单词背诵的过程中 ...

  3. Linux下安装MATLAB

    Why do I see "Preparing installation files ... Installing ... Finished..." in the terminal ...

  4. win10 修改hosts

    win10 修改hosts,是权限的问题, C:\Windows\System32\drivers\etc\hosts 添加权限即可. http://jingyan.baidu.com/article ...

  5. C++builder XML XSL 代码生成

    void __fastcall TFrmGenCode::XSLTxml1Click(TObject *Sender) { // XSLT转换xml文件格式 _di_IXMLDocument xml; ...

  6. generator+列表生成式输出杨辉三角形

    杨辉三角形: 1                  1    1              1    2    1         1     3    3     1    1      4    ...

  7. delphi datasnap

    http://blog.csdn.net/shuaihj/article/details/6129121 http://blog.csdn.net/ddqqyy/article/details/617 ...

  8. MapReduce Notes

    [MapReduce Notes] 1.一个Map/Reduce 作业的输入和输出类型如下所示: 2.Shuffle & Sort & Secondary Sort Reducer的输 ...

  9. wcf将一个服务同时绑定到http和tcp的写法

    服务器端:<?xml version="1.0" encoding="utf-8" ?><configuration>  <con ...

  10. 22-python爬虫解决gbk乱码问题

    转载自: python爬虫解决gbk乱码问题   今天尝试了下爬虫,爬取一本小说,忘语的凡人修仙仙界篇,当然这样不好,大家要支持正版. 爬取过程中是老套路,先获取网页源代码 # -*- coding: ...