基于ASP.NET生成条形码(code128)
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Drawing;
- using System.Data;
- namespace MvcGuestBook.Common
- {
- //条形码公共类
- public class BarCode128
- {
- private DataTable m_Code128 = new DataTable();
- private uint m_Height = ;
- /// <summary>
- /// 高度
- /// </summary>
- public uint Height { get { return m_Height; } set { m_Height = value; } }
- private Font m_ValueFont = null;
- /// <summary>
- /// 是否显示可见号码 如果为NULL不显示号码
- /// </summary>
- public Font ValueFont { get { return m_ValueFont; } set { m_ValueFont = value; } }
- private byte m_Magnify = ;
- /// <summary>
- /// 放大倍数
- /// </summary>
- public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } }
- /// <summary>
- /// 条码类别
- /// </summary>
- public enum Encode
- {
- Code128A,
- Code128B,
- Code128C,
- EAN128
- }
- public BarCode128()
- {
- m_Code128.Columns.Add("ID");
- m_Code128.Columns.Add("Code128A");
- m_Code128.Columns.Add("Code128B");
- m_Code128.Columns.Add("Code128C");
- m_Code128.Columns.Add("BandCode");
- m_Code128.CaseSensitive = true;
- #region 数据表
- m_Code128.Rows.Add("", " ", " ", "", "");
- m_Code128.Rows.Add("", "!", "!", "", "");
- m_Code128.Rows.Add("", "\"", "\"", "", "");
- m_Code128.Rows.Add("", "#", "#", "", "");
- m_Code128.Rows.Add("", "$", "$", "", "");
- m_Code128.Rows.Add("", "%", "%", "", "");
- m_Code128.Rows.Add("", "&", "&", "", "");
- m_Code128.Rows.Add("", "'", "'", "", "");
- m_Code128.Rows.Add("", "(", "(", "", "");
- m_Code128.Rows.Add("", ")", ")", "", "");
- m_Code128.Rows.Add("", "*", "*", "", "");
- m_Code128.Rows.Add("", "+", "+", "", "");
- m_Code128.Rows.Add("", ",", ",", "", "");
- m_Code128.Rows.Add("", "-", "-", "", "");
- m_Code128.Rows.Add("", ".", ".", "", "");
- m_Code128.Rows.Add("", "/", "/", "", "");
- m_Code128.Rows.Add("", "", "", "", "");
- m_Code128.Rows.Add("", "", "", "", "");
- m_Code128.Rows.Add("", "", "", "", "");
- m_Code128.Rows.Add("", "", "", "", "");
- m_Code128.Rows.Add("", "", "", "", "");
- m_Code128.Rows.Add("", "", "", "", "");
- m_Code128.Rows.Add("", "", "", "", "");
- m_Code128.Rows.Add("", "", "", "", "");
- m_Code128.Rows.Add("", "", "", "", "");
- m_Code128.Rows.Add("", "", "", "", "");
- m_Code128.Rows.Add("", ":", ":", "", "");
- m_Code128.Rows.Add("", ";", ";", "", "");
- m_Code128.Rows.Add("", "<", "<", "", "");
- m_Code128.Rows.Add("", "=", "=", "", "");
- m_Code128.Rows.Add("", ">", ">", "", "");
- m_Code128.Rows.Add("", "?", "?", "", "");
- m_Code128.Rows.Add("", "@", "@", "", "");
- m_Code128.Rows.Add("", "A", "A", "", "");
- m_Code128.Rows.Add("", "B", "B", "", "");
- m_Code128.Rows.Add("", "C", "C", "", "");
- m_Code128.Rows.Add("", "D", "D", "", "");
- m_Code128.Rows.Add("", "E", "E", "", "");
- m_Code128.Rows.Add("", "F", "F", "", "");
- m_Code128.Rows.Add("", "G", "G", "", "");
- m_Code128.Rows.Add("", "H", "H", "", "");
- m_Code128.Rows.Add("", "I", "I", "", "");
- m_Code128.Rows.Add("", "J", "J", "", "");
- m_Code128.Rows.Add("", "K", "K", "", "");
- m_Code128.Rows.Add("", "L", "L", "", "");
- m_Code128.Rows.Add("", "M", "M", "", "");
- m_Code128.Rows.Add("", "N", "N", "", "");
- m_Code128.Rows.Add("", "O", "O", "", "");
- m_Code128.Rows.Add("", "P", "P", "", "");
- m_Code128.Rows.Add("", "Q", "Q", "", "");
- m_Code128.Rows.Add("", "R", "R", "", "");
- m_Code128.Rows.Add("", "S", "S", "", "");
- m_Code128.Rows.Add("", "T", "T", "", "");
- m_Code128.Rows.Add("", "U", "U", "", "");
- m_Code128.Rows.Add("", "V", "V", "", "");
- m_Code128.Rows.Add("", "W", "W", "", "");
- m_Code128.Rows.Add("", "X", "X", "", "");
- m_Code128.Rows.Add("", "Y", "Y", "", "");
- m_Code128.Rows.Add("", "Z", "Z", "", "");
- m_Code128.Rows.Add("", "[", "[", "", "");
- m_Code128.Rows.Add("", "\\", "\\", "", "");
- m_Code128.Rows.Add("", "]", "]", "", "");
- m_Code128.Rows.Add("", "^", "^", "", "");
- m_Code128.Rows.Add("", "_", "_", "", "");
- m_Code128.Rows.Add("", "NUL", "`", "", "");
- m_Code128.Rows.Add("", "SOH", "a", "", "");
- m_Code128.Rows.Add("", "STX", "b", "", "");
- m_Code128.Rows.Add("", "ETX", "c", "", "");
- m_Code128.Rows.Add("", "EOT", "d", "", "");
- m_Code128.Rows.Add("", "ENQ", "e", "", "");
- m_Code128.Rows.Add("", "ACK", "f", "", "");
- m_Code128.Rows.Add("", "BEL", "g", "", "");
- m_Code128.Rows.Add("", "BS", "h", "", "");
- m_Code128.Rows.Add("", "HT", "i", "", "");
- m_Code128.Rows.Add("", "LF", "j", "", "");
- m_Code128.Rows.Add("", "VT", "k", "", "");
- m_Code128.Rows.Add("", "FF", "I", "", "");
- m_Code128.Rows.Add("", "CR", "m", "", "");
- m_Code128.Rows.Add("", "SO", "n", "", "");
- m_Code128.Rows.Add("", "SI", "o", "", "");
- m_Code128.Rows.Add("", "DLE", "p", "", "");
- m_Code128.Rows.Add("", "DC1", "q", "", "");
- m_Code128.Rows.Add("", "DC2", "r", "", "");
- m_Code128.Rows.Add("", "DC3", "s", "", "");
- m_Code128.Rows.Add("", "DC4", "t", "", "");
- m_Code128.Rows.Add("", "NAK", "u", "", "");
- m_Code128.Rows.Add("", "SYN", "v", "", "");
- m_Code128.Rows.Add("", "ETB", "w", "", "");
- m_Code128.Rows.Add("", "CAN", "x", "", "");
- m_Code128.Rows.Add("", "EM", "y", "", "");
- m_Code128.Rows.Add("", "SUB", "z", "", "");
- m_Code128.Rows.Add("", "ESC", "{", "", "");
- m_Code128.Rows.Add("", "FS", "|", "", "");
- m_Code128.Rows.Add("", "GS", "}", "", "");
- m_Code128.Rows.Add("", "RS", "~", "", "");
- m_Code128.Rows.Add("", "US", "DEL", "", "");
- m_Code128.Rows.Add("", "FNC3", "FNC3", "", "");
- m_Code128.Rows.Add("", "FNC2", "FNC2", "", "");
- m_Code128.Rows.Add("", "SHIFT", "SHIFT", "", "");
- m_Code128.Rows.Add("", "CODEC", "CODEC", "", "");
- m_Code128.Rows.Add("", "CODEB", "FNC4", "CODEB", "");
- m_Code128.Rows.Add("", "FNC4", "CODEA", "CODEA", "");
- m_Code128.Rows.Add("", "FNC1", "FNC1", "FNC1", "");
- m_Code128.Rows.Add("", "StartA", "StartA", "StartA", "");
- m_Code128.Rows.Add("", "StartB", "StartB", "StartB", "");
- m_Code128.Rows.Add("", "StartC", "StartC", "StartC", "");
- m_Code128.Rows.Add("", "Stop", "Stop", "Stop", "");
- #endregion
- }
- /// <summary>
- /// 获取128图形
- /// </summary>
- /// <param name="p_Text">文字</param>
- /// <param name="p_Code">编码</param>
- /// <returns>图形</returns>
- public Bitmap GetCodeImage(string p_Text, Encode p_Code)
- {
- string _ViewText = p_Text;
- string _Text = "";
- IList<int> _TextNumb = new List<int>();
- int _Examine = ; //首位
- switch (p_Code)
- {
- case Encode.Code128C:
- _Examine = ;
- if (!((p_Text.Length & ) == )) throw new Exception("128C长度必须是偶数");
- while (p_Text.Length != )
- {
- int _Temp = ;
- try
- {
- int _CodeNumb128 = Int32.Parse(p_Text.Substring(, ));
- }
- catch
- {
- throw new Exception("128C必须是数字!");
- }
- _Text += GetValue(p_Code, p_Text.Substring(, ), ref _Temp);
- _TextNumb.Add(_Temp);
- p_Text = p_Text.Remove(, );
- }
- break;
- case Encode.EAN128:
- _Examine = ;
- if (!((p_Text.Length & ) == )) throw new Exception("EAN128长度必须是偶数");
- _TextNumb.Add();
- _Text += "";
- while (p_Text.Length != )
- {
- int _Temp = ;
- try
- {
- int _CodeNumb128 = Int32.Parse(p_Text.Substring(, ));
- }
- catch
- {
- throw new Exception("128C必须是数字!");
- }
- _Text += GetValue(Encode.Code128C, p_Text.Substring(, ), ref _Temp);
- _TextNumb.Add(_Temp);
- p_Text = p_Text.Remove(, );
- }
- break;
- default:
- if (p_Code == Encode.Code128A)
- {
- _Examine = ;
- }
- else
- {
- _Examine = ;
- }
- while (p_Text.Length != )
- {
- int _Temp = ;
- string _ValueCode = GetValue(p_Code, p_Text.Substring(, ), ref _Temp);
- if (_ValueCode.Length == ) throw new Exception("无效的字符集!" + p_Text.Substring(, ).ToString());
- _Text += _ValueCode;
- _TextNumb.Add(_Temp);
- p_Text = p_Text.Remove(, );
- }
- break;
- }
- if (_TextNumb.Count == ) throw new Exception("错误的编码,无数据");
- _Text = _Text.Insert(, GetValue(_Examine)); //获取开始位
- for (int i = ; i != _TextNumb.Count; i++)
- {
- _Examine += _TextNumb[i] * (i + );
- }
- _Examine = _Examine % ; //获得严效位
- _Text += GetValue(_Examine); //获取严效位
- _Text += ""; //结束位
- Bitmap _CodeImage = GetImage(_Text);
- GetViewText(_CodeImage, _ViewText);
- return _CodeImage;
- }
- /// <summary>
- /// 获取目标对应的数据
- /// </summary>
- /// <param name="p_Code">编码</param>
- /// <param name="p_Value">数值 A b 30</param>
- /// <param name="p_SetID">返回编号</param>
- /// <returns>编码</returns>
- private string GetValue(Encode p_Code, string p_Value, ref int p_SetID)
- {
- if (m_Code128 == null) return "";
- DataRow[] _Row = m_Code128.Select(p_Code.ToString() + "='" + p_Value + "'");
- if (_Row.Length != ) throw new Exception("错误的编码" + p_Value.ToString());
- p_SetID = Int32.Parse(_Row[]["ID"].ToString());
- return _Row[]["BandCode"].ToString();
- }
- /// <summary>
- /// 根据编号获得条纹
- /// </summary>
- /// <param name="p_CodeId"></param>
- /// <returns></returns>
- private string GetValue(int p_CodeId)
- {
- DataRow[] _Row = m_Code128.Select("ID='" + p_CodeId.ToString() + "'");
- if (_Row.Length != ) throw new Exception("验效位的编码错误" + p_CodeId.ToString());
- return _Row[]["BandCode"].ToString();
- }
- /// <summary>
- /// 获得条码图形
- /// </summary>
- /// <param name="p_Text">文字</param>
- /// <returns>图形</returns>
- private Bitmap GetImage(string p_Text)
- {
- char[] _Value = p_Text.ToCharArray();
- int _Width = ;
- for (int i = ; i != _Value.Length; i++)
- {
- _Width += Int32.Parse(_Value[i].ToString()) * (m_Magnify + );
- }
- Bitmap _CodeImage = new Bitmap(_Width, (int)m_Height);
- Graphics _Garphics = Graphics.FromImage(_CodeImage);
- //Pen _Pen;
- int _LenEx = ;
- for (int i = ; i != _Value.Length; i++)
- {
- int _ValueNumb = Int32.Parse(_Value[i].ToString()) * (m_Magnify + ); //获取宽和放大系数
- if (!((i & ) == ))
- {
- //_Pen = new Pen(Brushes.White, _ValueNumb);
- _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, , _ValueNumb, (int)m_Height));
- }
- else
- {
- //_Pen = new Pen(Brushes.Black, _ValueNumb);
- _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, , _ValueNumb, (int)m_Height));
- }
- //_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
- _LenEx += _ValueNumb;
- }
- _Garphics.Dispose();
- return _CodeImage;
- }
- /// <summary>
- /// 显示可见条码文字 如果小于40 不显示文字
- /// </summary>
- /// <param name="p_Bitmap">图形</param>
- private void GetViewText(Bitmap p_Bitmap, string p_ViewText)
- {
- if (m_ValueFont == null) return;
- Graphics _Graphics = Graphics.FromImage(p_Bitmap);
- SizeF _DrawSize = _Graphics.MeasureString(p_ViewText, m_ValueFont);
- if (_DrawSize.Height > p_Bitmap.Height - || _DrawSize.Width > p_Bitmap.Width)
- {
- _Graphics.Dispose();
- return;
- }
- int _StarX = p_Bitmap.Width - (int)_DrawSize.Width;
- int _StarY = p_Bitmap.Height - (int)_DrawSize.Height;
- _Graphics.FillRectangle(Brushes.White, new Rectangle(, _StarY, p_Bitmap.Width, (int)_DrawSize.Height));
- _Graphics.DrawString(p_ViewText, m_ValueFont, Brushes.Black, _StarX / , _StarY);//文字在图片的中间位置
- }
- //12345678
- //(105 + (1 * 12 + 2 * 34 + 3 * 56 + 4 *78)) % 103 = 47
- //结果为starc +12 +34 +56 +78 +47 +end
- internal Image GetCodeImage(string p)
- {
- throw new NotImplementedException();
- }
- }
- }
- //调用条形码方法
- public ActionResult BarCode()
- {
- BarCode128 code = new BarCode128();
- code.ValueFont = new System.Drawing.Font("Arial", );//声明条码下方的字体
- Bitmap bitmap = code.GetCodeImage("", BarCode128.Encode.Code128A);//通过对文本框的文件进行Code128编码获得位图
- string WebPath = "FlexPaper/BarCodeFile/" + DateTime.Now.ToString("yyyy-MM-dd");
- string filepath = Server.MapPath(WebPath);
- if (!Directory.Exists(filepath))
- Directory.CreateDirectory(filepath);
- string FileName = DateTime.Now.ToString("HHmmss") + ".jpg";
- bitmap.Save(filepath + "/" + FileName, ImageFormat.Jpeg);
- TempData["BarCode11"] = WebPath + "/" + FileName;
- return RedirectToAction("Index");
- }
- <% if (TempData["BarCode11"] != null)
- { %>
- <img src="/Message/<%: TempData["BarCode11"] %>" alt="BarCode" />
- <%} %>
- <%: Html.ActionLink("生成条形码", "BarCode")%>
基于ASP.NET生成条形码(code128)的更多相关文章
- 使用BarcodeLib.Barcode.ASP.NET生成条形码
生成条形码图片,然后在前台页面展示: <img id="img" src="Mobile/<%=url %>"/> public str ...
- C#生成条形码 Code128算法
条形有很多种,Code128是比较常用的一种,是一种高密度条码, CODE128 码可表示从 ASCII 0 到ASCII 127 共128个字符,故称128码.其中包含了数字.字母和符号字符. Co ...
- 基于ASP.NET生成二维码详细源码
详细链接:https://shop499704308.taobao.com/?spm=a1z38n.10677092.card.11.594c1debsAGeakusing System; using ...
- asp.net 生成、解析条形码和二维码
原文 asp.net 生成.解析条形码和二维码 一.条形码 一维码,俗称条形码,广泛的用于电子工业等行业.比如我们常见的书籍背面就会有条形码,通过扫描枪等设备扫描就可以获得书籍的ISBN(Intern ...
- 基于Asp.Net Core,利用ZXing来生成二维码的一般流程
本文主要介绍如何在.net环境下,基于Asp.Net Core,利用ZXing来生成二维码的一般操作.对二维码工作原理了解,详情见:https://blog.csdn.net/weixin_36191 ...
- C# 在Word文档中生成条形码
C# 在Word文档中生成条形码 简介 条形码是由多个不同的空白和黑条按照一定的顺序组成,用于表示各种信息如产品名称.制造商.类别.价格等.目前,条形码在我们的日常生活中有着很广泛的应用,不管是在图书 ...
- 使用html2canvas实现批量生成条形码
/*前台代码*/ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Generat ...
- JAVA生成条形码
1.下载生成条形码所需要的jar包barcode4j.jar: 2.java生成条形码代码 import java.awt.image.BufferedImage;import java.io.Fil ...
- C# 生成条形码
原文地址:http://www.cnblogs.com/xcsn/p/4514759.html 引用BarcodeLib.dll(百度云中有)生成条形 protected void Button2_C ...
随机推荐
- springboot 2.0 自动装配原理 以redis为例
当面试管问你springboot 和 普通spring 有什么区别? 您还在回答: 简化了配置 ,内置tomcat 等等 吗 ? 那只是皮毛, 最重要的还是自动化配置.一起来了解一下 第一步: 第二步 ...
- 20165233 学习基础和C语言基础调查
学习基础与<做中学>阅读心得 读<做中学>有感 娄老师通过在学习工作中总结出的"做中学"的方式,将其运用到减肥.五笔训练.乒乓球训练以及英文单词背诵的过程中 ...
- Linux下安装MATLAB
Why do I see "Preparing installation files ... Installing ... Finished..." in the terminal ...
- win10 修改hosts
win10 修改hosts,是权限的问题, C:\Windows\System32\drivers\etc\hosts 添加权限即可. http://jingyan.baidu.com/article ...
- C++builder XML XSL 代码生成
void __fastcall TFrmGenCode::XSLTxml1Click(TObject *Sender) { // XSLT转换xml文件格式 _di_IXMLDocument xml; ...
- generator+列表生成式输出杨辉三角形
杨辉三角形: 1 1 1 1 2 1 1 3 3 1 1 4 ...
- delphi datasnap
http://blog.csdn.net/shuaihj/article/details/6129121 http://blog.csdn.net/ddqqyy/article/details/617 ...
- MapReduce Notes
[MapReduce Notes] 1.一个Map/Reduce 作业的输入和输出类型如下所示: 2.Shuffle & Sort & Secondary Sort Reducer的输 ...
- wcf将一个服务同时绑定到http和tcp的写法
服务器端:<?xml version="1.0" encoding="utf-8" ?><configuration> <con ...
- 22-python爬虫解决gbk乱码问题
转载自: python爬虫解决gbk乱码问题 今天尝试了下爬虫,爬取一本小说,忘语的凡人修仙仙界篇,当然这样不好,大家要支持正版. 爬取过程中是老套路,先获取网页源代码 # -*- coding: ...