基于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 ...
随机推荐
- mybatis一对多关联查询+pagehelper->分页错误
mybatis一对多关联查询+pagehelper->分页错误. 现象: 网上其他人遇到的类似问题:https://segmentfault.com/q/1010000009692585 解决: ...
- requests and BeautifulSoup
requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...
- OleVariant Variant
OleVariant ArrayDimCount OleVariant; System.Variants.hpp 判断OleVariant 是否为空 System::OleVariant ov i ...
- ajax传参里含有特殊字符的坑
问题场景:今天在测试自己手上的页面功能时,发现一个小bug,在用ajax向后台发数据时,只要参数中出现一些特殊字符,控制台会报错http 400的问题,其实就是特殊字符服务器不能解析.好了,问题是找到 ...
- Configure、中间件与ErrorHandlingMiddleware全局异常捕获
一.Configure Startup.cs中的Configure方法主要是http处理管道配置.中间件和一些系统配置,其中 IApplicationBuilder: 定义一个类,该类提供配置应用程序 ...
- SIEBEL GET最新时提示表异常
无法GET最新,则将此表GET一次,CHECK OUT下来,再UNDO CHECK IN即可
- Volley的简单封装
算了一下,好像有很久没有写博客了.其实,关于写博客这件事,我从来没有把他当成我的一种任务,而是在学习过程中的一种总结和自我发现,同样也是为了练一练文笔,说不定有一天,我也能出一本书像<第一行代码 ...
- [leetcode]621. Task Scheduler任务调度
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...
- loadrunner12: Error -27492: "HttpSendRequest" failed, Windows error code=8
这个问题我在网上看到有这样的解释:1.timeout时间超时设置问题2.Run-Time Settings -> Preferences -> Advanced. 确定此选项未被选中:&q ...
- Cook-Torrence Illumination Model 的一些数学说明
Cook-Torrence 光照模型如下: 这个Io就是计算后最终的光强,主要是用来计算镜面反射光,漫反射和环境光的计算和Phong模型一致. F:Fresnel反射系数.主要用来说明反射光强度占入射 ...