[ASP.NET] 在页面上显示Code39码(一维条码)
时间:2019-09-09
本文章向大家介绍[ASP.NET] 在页面上显示Code39码(一维条码),主要包括[ASP.NET] 在页面上显示Code39码(一维条码)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
摘要:[ASP.NET] 在页面上显示Code39码(一维条码)
最近小弟公司刚好遇到有需要输入字符串后
转换成维条码并显示在页面上
应此小弟参考一下网络许多做法
也都大同小异
Step 1: 建立code39.ashx
<%@ WebHandler Language="C#" %>
using System;
using System.Web;
using System.Drawing;
using System.IO;
public class code39 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string sCode = string.Empty;
//清除该页输出缓存,设置该页无缓存
context.Response.Buffer = true;
context.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
context.Response.Expires = 0;
context.Response.CacheControl = "no-cache";
context.Response.AppendHeader("Pragma", "No-Cache");
//将Code39条码写入内存流,并将其以 "image/Png" 格式输出
MemoryStream oStream = new MemoryStream();
try
{
Bitmap oBmp = GetCode39(context.Request.QueryString["id"]);
oBmp.Save(oStream, System.Drawing.Imaging.ImageFormat.Png);
oBmp.Dispose();
context.Response.ClearContent();
context.Response.ContentType = "image/Png";
context.Response.BinaryWrite(oStream.ToArray());
}
finally
{
//释放资源
oStream.Dispose();
}
}
public bool IsReusable
{
get
{
return false;
}
}
private Bitmap GetCode39(string strSource)
{
int x = 5; //左边界
int y = 0; //上边界
int WidLength = 2; //粗BarCode长度
int NarrowLength = 1; //细BarCode长度
int BarCodeHeight = 24; //BarCode高度
int intSourceLength = strSource.Length;
string strEncode = "010010100"; //编码字符串 初值为 起始符号 *
string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; //Code39的字母
string[] Code39 = //Code39的各字母对应码
{
///* 0 */ "000110100",
///* 1 */ "100100001",
///* 2 */ "001100001",
///* 3 */ "101100000",
///* 4 */ "000110001",
///* 5 */ "100110000",
///* 6 */ "001110000",
///* 7 */ "000100101",
///* 8 */ "100100100",
///* 9 */ "001100100",
///* A */ "100001001",
///* B */ "001001001",
///* C */ "101001000",
///* D */ "000011001",
///* E */ "100011000",
///* F */ "001011000",
///* G */ "000001101",
///* H */ "100001100",
///* I */ "001001100",
///* J */ "000011100",
///* K */ "100000011",
///* L */ "001000011",
///* M */ "101000010",
///* N */ "000010011",
///* O */ "100010010",
///* P */ "001010010",
///* Q */ "000000111",
///* R */ "100000110",
///* S */ "001000110",
///* T */ "000010110",
///* U */ "110000001",
///* V */ "011000001",
///* W */ "111000000",
///* X */ "010010001",
///* Y */ "110010000",
///* Z */ "011010000",
///* - */ "010000101",
///* . */ "110000100",
///*' '*/ "011000100",
///* $ */ "010101000",
///* / */ "010100010",
///* + */ "010001010",
///* % */ "000101010",
///* * */ "010010100"
};
strSource = strSource.ToUpper();
//实践图片
Bitmap objBitmap = new Bitmap(((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2), BarCodeHeight + (y * 2));
Graphics objGraphics = Graphics.FromImage(objBitmap); //声明GDI+绘图界面
//填上底色
objGraphics.FillRectangle(Brushes.White, 0, 0, objBitmap.Width, objBitmap.Height);
for (int i = 0; i < intSourceLength; i++)
{
//检查是否有非法符
if (AlphaBet.IndexOf(strSource[i]) == -1 || strSource[i] == '*')
{
objGraphics.DrawString("含有非法符", SystemFonts.DefaultFont, Brushes.Red, x, y);
return objBitmap;
}
//查表编码
strEncode = string.Format("{0}0{1}", strEncode, Code39[AlphaBet.IndexOf(strSource[i])]);
}
strEncode = string.Format("{0}0010010100", strEncode); //补上结束符号 *
int intEncodeLength = strEncode.Length; //编码后长度
int intBarWidth;
for (int i = 0; i < intEncodeLength; i++) //依码画出Code39 BarCode
{
intBarWidth = strEncode[i] == '1' ? WidLength : NarrowLength;
objGraphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White, x, y, intBarWidth, BarCodeHeight);
x += intBarWidth;
}
return objBitmap;
}
}
Step 2:调用使用
// 前端写法,加入image为显式条码用,textbox为输入字符串
// 后台,将Image1的ImageUrl指定为code39.ashx,并使用网页传值方式得到回传值
protected void Button4_Click(object sender, EventArgs e)
{
string sUrl = "~/code39.ashx?https://az787680.vo.msecnd.net/user/aquarius6913/1304/2013481002500.JPG" rel="lightbox">
Y2J's Life:http://kimenyeh.blogspot.tw/ 原文:大专栏 [ASP.NET] 在页面上显示Code39码(一维条码)
原文地址:https://www.cnblogs.com/petewell/p/11490050.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 02 . Zabbix配置监控项及聚合图形
- 01 . GitLab简介及环境部署
- 03 . Prometheus监控容器和HTTP探针应用及服务发现
- java编程思想第四版第九章习题
- 03 . Django之腾讯云短信
- ESP32 MQTT连接到中移OneNET物联网平台(附源码)
- 01 . Docker原理部署及常用操作命令
- SSH原理常见应用升级及端口转发
- 01 . Linux常用命令
- 私人订制属于自己的Linux系统
- 04 . Docker安全与Docker底层实现
- 03 . Docker数据资源管理与网络
- 02 . DockerFile构建镜像和Docker仓库
- Mysql通过MHA实现高可用
- ProxySQL简介原理及读写分离应用