using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace JXUtil
{
public class BarCode
{
private Hashtable _Code39 = new Hashtable(); /// <summary>
/// 内容
/// </summary>
public string Text { get; set; } /// <summary>
/// 放大倍数
/// </summary>
public byte Magnify { get; set; } /// <summary>
/// 图形高
/// </summary>
public int Height { get; set; } /// <summary>
/// 字体大小
/// </summary>
public Font ViewFont { get; set; } public BarCode()
{
_Code39.Add("A", "");
_Code39.Add("B", "");
_Code39.Add("C", "");
_Code39.Add("D", "");
_Code39.Add("E", "");
_Code39.Add("F", "");
_Code39.Add("G", "");
_Code39.Add("H", "");
_Code39.Add("I", "");
_Code39.Add("J", "");
_Code39.Add("K", "");
_Code39.Add("L", "");
_Code39.Add("M", "");
_Code39.Add("N", "");
_Code39.Add("O", "");
_Code39.Add("P", "");
_Code39.Add("Q", "");
_Code39.Add("R", "");
_Code39.Add("S", "");
_Code39.Add("T", "");
_Code39.Add("U", "");
_Code39.Add("V", "");
_Code39.Add("W", "");
_Code39.Add("X", "");
_Code39.Add("Y", "");
_Code39.Add("Z", "");
_Code39.Add("", "");
_Code39.Add("", "");
_Code39.Add("", "");
_Code39.Add("", "");
_Code39.Add("", "");
_Code39.Add("", "");
_Code39.Add("", "");
_Code39.Add("", "");
_Code39.Add("", "");
_Code39.Add("", "");
_Code39.Add("+", "");
_Code39.Add("-", "");
_Code39.Add("*", "");
_Code39.Add("/", "");
_Code39.Add("%", "");
_Code39.Add("&", "");
_Code39.Add(".", "");
_Code39.Add(" ", "");
} public enum Code39Model
{
/// <summary>
/// 基本类别 1234567890ABC
/// </summary>
Code39Normal,
/// <summary>
/// 全ASCII方式 +A+B 来表示小写
/// </summary>
Code39FullAscII
} /// <summary>
/// 获得条码图形
/// </summary>
/// <param name="text">文字信息</param>
/// <param name="model">类别</param>
/// <param name="stat">是否增加前后*号</param>
/// <returns>图形</returns>
public Bitmap GetCodeImage(Code39Model model, bool star)
{
string textVal = "";
string textCode = "";
char[] charVal = null;
switch (model)
{
case Code39Model.Code39Normal:
textVal = Text.ToUpper();
break;
default:
charVal = Text.ToCharArray();
for (int i = ; i != charVal.Length; i++)
{
if ((int)charVal[i] >= && (int)charVal[i] <= )
{
textVal += "+" + charVal[i].ToString().ToUpper(); }
else
{
textVal += charVal[i].ToString();
}
}
break;
}
charVal = textVal.ToCharArray();
if (star == true) textCode += _Code39["*"];
for (int i = ; i != charVal.Length; i++)
{
if (star == true && charVal[i] == '*') throw new Exception("带有起始符号不能出现*");
object _CharCode = _Code39[charVal[i].ToString()];
if (_CharCode == null) throw new Exception("不可用的字符" + charVal[i].ToString());
textCode += _CharCode.ToString();
}
if (star == true) textCode += _Code39["*"];
Bitmap bmp = GetImage(textCode);
GetViewImage(bmp, Text);
return bmp;
} /// <summary>
/// 绘制编码图形
/// </summary>
/// <param name="text">编码</param>
/// <returns>图形</returns>
private Bitmap GetImage(string text)
{
char[] val = text.ToCharArray(); //宽 == 需要绘制的数量*放大倍数 + 两个字的宽
Bitmap codeImg = new Bitmap(val.Length * ((int)Magnify + ), (int)Height);
Graphics graph = Graphics.FromImage(codeImg); graph.FillRectangle(Brushes.White, new Rectangle(, , codeImg.Width, codeImg.Height)); int len = ;
for (int i = ; i != val.Length; i++)
{
int width = Magnify + ;
if (val[i] == '')
{
graph.FillRectangle(Brushes.Black, new Rectangle(len, , width, Height)); }
else
{
graph.FillRectangle(Brushes.White, new Rectangle(len, , width, Height));
}
len += width;
} graph.Dispose();
return codeImg;
} /// <summary>
/// 绘制文字
/// </summary>
/// <param name="codeImage">图形</param>
/// <param name="text">文字</param>
private void GetViewImage(Bitmap codeImage, string text)
{
if (ViewFont == null) return;
Graphics graphic = Graphics.FromImage(codeImage);
SizeF fontSize = graphic.MeasureString(text, ViewFont); if (fontSize.Width > codeImage.Width || fontSize.Height > codeImage.Height - )
{
graphic.Dispose();
return;
}
int starHeight = codeImage.Height - (int)fontSize.Height;
graphic.FillRectangle(Brushes.White, new Rectangle(, starHeight, codeImage.Width, (int)fontSize.Height)); int _StarWidth = (codeImage.Width - (int)fontSize.Width) / ;
graphic.DrawString(text, ViewFont, Brushes.Black, _StarWidth, starHeight);
graphic.Dispose(); }
} public class BarCode128
{
// ASCII从32到127对应的条码区,由3个条、3个空、共11个单元构成,符号内含校验码
private string[] Code128Encoding = new string[] {
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", ""
};
private const string Code128Stop = "", Code128End = ""; //固定码尾
private enum Code128ChangeModes { CodeA = , CodeB = , CodeC = }; //变更
private enum Code128StartModes { CodeUnset = , CodeA = , CodeB = , CodeC = };//各类编码的码头 /// <summary>
/// 绘制Code128码(以像素为单位)
/// </summary>
public int EncodeBarcode(string code, System.Drawing.Graphics g, int x, int y, int width, int height, bool showText)
{
if (string.IsNullOrEmpty(code)) new Exception("条码不能为空");
List<int> encoded = CodetoEncoded(code); //1.拆分转义
encoded.Add(CheckDigitCode128(encoded)); //2.加入校验码
string encodestring = EncodeString(encoded); //3.编码 if (showText) //计算文本的大小,字体占图像的1/4高
{
Font font = new System.Drawing.Font("宋体", height / 4F, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel, ((byte)()));
SizeF size = g.MeasureString(code, font);
height = height - (int)size.Height; int _StarWidth = (width - (int)size.Width) / ;
g.DrawString(code, font, System.Drawing.Brushes.Black, _StarWidth, height);
int w = DrawBarCode(g, encodestring, x, y, width, height); //4.绘制
return ((int)size.Width > w ? (int)size.Width : w);
}
else
return DrawBarCode(g, encodestring, x, y, width, height); //4.绘制
} //1.检测并将字符串拆分并加入码头
private List<int> CodetoEncoded(string code)
{
List<int> encoded = new List<int>();
int type = ;//2:B类,3:C类
for (int i = ; code.Length > ; i++)
{
int k = isNumber(code);
if (k >= ) //连续偶个数字可优先使用C类(其实并不定要转C类,但能用C类时条码会更短)
{
if (type == ) encoded.Add((int)Code128StartModes.CodeC); //加入码头
else if (type != ) encoded.Add((int)(Code128ChangeModes.CodeC)); //转义
type = ;
for (int j = ; j < k; j = j + ) //两位数字合为一个码身
{
encoded.Add(Int32.Parse(code.Substring(, )));
code = code.Substring();
}
}
else
{
if ((int)code[] < || (int)code[] > ) throw new Exception("字符串必须是数字或字母");
if (type == ) encoded.Add((int)Code128StartModes.CodeB); //加入码头
else if (type != ) encoded.Add((int)(Code128ChangeModes.CodeB)); //转义
type = ;
encoded.Add((int)code[] - );//字符串转为ASCII-32
code = code.Substring();
}
}
return encoded;
}
//2.校验码
private int CheckDigitCode128(List<int> encoded)
{
int check = encoded[];
for (int i = ; i < encoded.Count; i++)
check = check + (encoded[i] * i);
return (check % );
} //2.编码(对应Code128Encoding数组)
private string EncodeString(List<int> encoded)
{
string encodedString = "";
for (int i = ; i < encoded.Count; i++)
{
encodedString += Code128Encoding[encoded[i]];
}
encodedString += Code128Stop + Code128End; // 加入结束码
return encodedString;
} //4.绘制条码(返回实际图像宽度)
private int DrawBarCode(System.Drawing.Graphics g, string encodeString, int x, int y, int width, int height)
{
int w = width / encodeString.Length;
for (int i = ; i < encodeString.Length; i++)
{
g.FillRectangle(encodeString[i] == '' ? System.Drawing.Brushes.White : System.Drawing.Brushes.Black, x, y, w, height);
x += w;
}
return w * (encodeString.Length + );
}
//检测是否连续偶个数字,返回连续数字的长度
private int isNumber(string code)
{
int k = ;
for (int i = ; i < code.Length; i++)
{
if (char.IsNumber(code[i]))
k++;
else
break;
}
if (k % != ) k--;
return k;
} /// <summary>
/// 绘制Code128码到图片
/// </summary>
public Image EncodeBarcode(string code, int width, int height, bool showText)
{
Bitmap image = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(image))
{
g.Clear(Color.White);
int w = EncodeBarcode(code, g, , , width, height, showText); Bitmap image2 = new Bitmap(w, height); //剪切多余的空白;
using (Graphics g2 = Graphics.FromImage(image2))
{
g2.DrawImage(image, , );
return image2;
} } } /// <summary>
/// 绘制Code128码到流
/// </summary>
public byte[] EncodeBarcodeByte(string code, int width, int height, bool showText)
{
Image image = EncodeBarcode(code, width, height, showText);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] byteImage = ms.ToArray();
ms.Close();
image.Dispose();
return byteImage; }
}
}
 <div>
<table>
<tr>
<td>条码:</td>
<td>
<textarea id="barCode"></textarea><br />
<span style="color: #ccc;">动态生成条码,生成多条码使用“,”号分隔。</span>
</td>
</tr>
<tr>
<td></td>
<td>
<a id="btnCreate">生成</a>
</td>
</tr>
</table>
</div>
 $("#btnCreate").click(function () {
var barCode = $("#barCode").val();
if (barCode != "" && barCode != null) {
$("#print").html("");
var imgStr = "";
if (barCode.indexOf(",") > -1) {
var code = barCode.split(",");
$.each(code, function (index, value) {
if (value != "" && value != null) {
imgStr += "<div><img src='/BaseData/CreateBarCode?code=" + value + "'/></div>";
}
});
}
else {
imgStr = "<div><img src='/BaseData/CreateBarCode?code=" + barCode + "'/></div>";
} $("#print").append(imgStr);
}
else {
alert("条码不能为空!");
}
});
 public ActionResult CreateBarCode(string code)
{
JXUtil.BarCode barcode = new JXUtil.BarCode();
barcode.Text = code;
barcode.Height = ;
barcode.Magnify = ;
barcode.ViewFont = new Font("宋体", );
System.Drawing.Image codeImage = barcode.GetCodeImage(JXUtil.BarCode.Code39Model.Code39Normal, true); System.IO.MemoryStream ms = new System.IO.MemoryStream();
codeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ContentType = "image/jpeg";
Response.Clear();
Response.BinaryWrite(ms.ToArray()); return new EmptyResult();
}

asp.net mvc 生成条形码的更多相关文章

  1. ASP.Net MVC 生成安全验证码

    ---------html <td>验证码:</td>            <td>                <img src="/Logi ...

  2. ASP.NET MVC生成安全验证码

    html部分: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  3. (一)【转】asp.net mvc生成验证码

    网站添加验证码,主要为防止机器人程序批量注册,或对特定的注册用户用特定程序暴力破解方式,以进行不断的登录.灌水等危害网站的操作.验证码被广泛应用在注册.登录.留言等提交信息到服务器端处理的页面中.   ...

  4. Asp.net mvc生成验证码

    1.生成验证码类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  5. ASP.NET MVC 生成验证码

    using System.Web.Mvc; using System.Drawing; using System; using System.Drawing.Imaging; using Models ...

  6. ASP.NET MVC 生成EML文件

    需求: 点发送邮件按钮的时候, 自动在客户端电脑打开默认邮件的窗口,并且把内容和附件都附加上去. 解决方案: 尝试使用过Microsoft.Office.Interop.Outlook 和 MPAI. ...

  7. Asp.net MVC 生成zip并下载

    前面有生成Excel或Word的示例,所以就不再重新写了. 这里只提供将指定文件以ZIP的方式下载. 创建一个 Zip工具类 public class ZIPCompressUtil { public ...

  8. asp.net mvc 生成二维码

    生成二维码,帮助类: using Gma.QrCodeNet.Encoding; using Gma.QrCodeNet.Encoding.Windows.Render; using System; ...

  9. ASP.NET MVC生成静态页面

    1.先付上封装好生成静态页的原代码: public class Common { #region 获取模板页的Html代码 /// <summary> /// 获取页面的Html代码 // ...

随机推荐

  1. hdu 4930 Fighting the Landlords--2014 Multi-University Training Contest 6

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4930 Fighting the Landlords Time Limit: 2000/1000 MS ...

  2. Unity sqlite学习笔记一

    1.SQLITE的常识 SQLite是一个开源免费的数据库,一般用于嵌入系统或者小规模的应用软件开发中,你可以像使用Access一样使用它. sqlite的主要优点:零配置(Zero Configur ...

  3. Vlc基础数据结构记录

    1.  Vlc基础数据结构 hongxianzhao@hotmail.com 1.1  基础数据结构 struct vlc_object_t,相关文件为src\misc\objects.c. 定义为: ...

  4. 关于asp.net简单的下载问题

    关于asp.net的下载,只需将打开相应的文件路径就能在浏览器上实现下载功能,比如项目的同级目录上有一个文件 苍老师.zip<a href="苍老师.zip">< ...

  5. WIFI网络访问(一)

    一,WIFI 网卡有哪些状态? WIFI 总共有以下五个状态,实际就是一些整形常量: 1.   WIFI_STATE_DISABLED : WIFI 不能使用,其值是: 1 . 2.   WIFI_S ...

  6. 注册宝第五期beta2插件模块下载及说明

    原文:http://bbs.84zcb.com/showtopic-1882.aspx [软件名称]:注册宝插件模块 [软件版本]:V1.4 [软件大小]:6.36M [软件语言]:简体中文 [授权方 ...

  7. 正确合理的建立MYSQL数据库索引

    写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储100条记录.如果没有索引,查询将对整 ...

  8. Linux下MySql出现#1036 – Table ‘ ‘ is read only 错误解决方法

    本文为转载内容,感谢原作者.原文出自:http://zhaoxiaoru39.blog.163.com/blog/static/609552192012511104730115/ 我遇到的问题是:在n ...

  9. JSON之三:获取JSON文本并解释(以google的天气API为例)

    google提供了天气的api,以广州天气为例,地址为: http://api.openweathermap.org/data/2.5/weather?q=guangzhou 返回的结果为: {   ...

  10. AngularJS 深入理解 $scope 转载▼

    AngularJS 深入理解 $scope 转载▼ (2015-04-07 14:09:50)     $scope 的使用贯穿整个 AngularJS App 应用,它与数据模型相关联,同时也是表达 ...