C# 一维码生成
概念
一维条码即指条码条和空的排列规则,常用的一维码的码制包括:EAN码、39码、交叉25码、UPC码、128码、93码,ISBN码,及Codabar(库德巴码)等。
条形码起源于 20 世纪 40 年代,应用于 70 年代,普及于 80 年代。条码技术是在计算机应用和实践中产生并发展起来的广泛应用于商业、邮政、图书管理、仓储、工业生产过程控制、交通等领域的一种自动识别技术,具有输入速度快、准确度高、成本低、可靠性强等优点,在当今的自动识别技术中占有重要的地位。
信息全部为数字,主要应用于商品标识。
源码与实现
code39的实现
/// <summary>
/// 生成条码 Bitmap,自定义条码高度,自定义文字对齐样式
/// </summary>
/// <param name="sourceCode"></param>
/// <param name="barCodeHeight"></param>
/// <param name="sf"></param>
/// <returns></returns>
public Bitmap GetCode39(string sourceCode, int barCodeHeight, StringFormat sf)
{
BarCodeText = sourceCode.ToUpper();
int leftMargin = ;
int topMargin = ;
int thickLength = ;
int narrowLength = ;
int intSourceLength = sourceCode.Length;
string strEncode = "" ; //添加起始码“ *”.
var font = new System.Drawing.Font( "Segoe UI", );
string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*" ;
string[] Code39 =
{
/* 0 */ "" ,
/* 1 */ "" ,
/* 2 */ "" ,
/* 3 */ "" ,
/* 4 */ "" ,
/* 5 */ "" ,
/* 6 */ "" ,
/* 7 */ "" ,
/* 8 */ "" ,
/* 9 */ "" ,
/* A */ "" ,
/* B */ "" ,
/* C */ "" ,
/* D */ "" ,
/* E */ "" ,
/* F */ "" ,
/* G */ "" ,
/* H */ "" ,
/* I */ "" ,
/* J */ "" ,
/* K */ "" ,
/* L */ "" ,
/* M */ "" ,
/* N */ "" ,
/* O */ "" ,
/* P */ "" ,
/* Q */ "" ,
/* R */ "" ,
/* S */ "" ,
/* T */ "" ,
/* U */ "" ,
/* V */ "" ,
/* W */ "" ,
/* X */ "" ,
/* Y */ "" ,
/* Z */ "" ,
/* - */ "" ,
/* . */ "" ,
/*' '*/ "" ,
/* $ */ "" ,
/* / */ "" ,
/* + */ "" ,
/* % */ "" ,
/* * */ ""
};
sourceCode = sourceCode.ToUpper();
Bitmap objBitmap = new Bitmap(((thickLength * + narrowLength * ) * (intSourceLength + )) +
(leftMargin * ), barCodeHeight + (topMargin * ));
Graphics objGraphics = Graphics.FromImage(objBitmap);
objGraphics.FillRectangle( Brushes.White, , , objBitmap.Width, objBitmap.Height);
for (int i = ; i < intSourceLength; i++)
{
//非法字符校验
if (AlphaBet.IndexOf(sourceCode[i]) == - || sourceCode[i] == '*' )
{
objGraphics.DrawString( "Invalid Bar Code", SystemFonts.DefaultFont, Brushes .Red, leftMargin, topMargin);
return objBitmap;
}
//编码
strEncode = string.Format("{0}0{1}" , strEncode,
Code39[AlphaBet.IndexOf(sourceCode[i])]);
}
strEncode = string.Format("{0}0010010100" , strEncode); //添加结束码“*”
int intEncodeLength = strEncode.Length;
int intBarWidth;
for (int i = ; i < intEncodeLength; i++) //绘制 Code39 barcode
{
intBarWidth = strEncode[i] == '' ? thickLength : narrowLength;
objGraphics.FillRectangle(i % == ? Brushes.Black : Brushes .White, leftMargin, topMargin, intBarWidth, barCodeHeight);
leftMargin += intBarWidth;
}
//绘制明码
Font barCodeTextFont = new Font( "黑体" , 10F);
RectangleF rect = new RectangleF(, barCodeHeight - , objBitmap.Width - , );
objGraphics.FillRectangle( Brushes.White, rect);
//文本对齐
objGraphics.DrawString(BarCodeText, barCodeTextFont, Brushes.Black, rect, sf);
return objBitmap;
}
Demo2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Threading; namespace BarCodeTest
{
public partial class Form1 : Form
{
string inputString = "";
code128 barcode = new code128();
Thread thre; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
//label14.Location = new Point((panel1.Width - label14.Width) / 2, 92); /*
label6.Font = new Font("宋体", 12);
label8.Font = new Font("宋体", 12);
label9.Font = new Font("宋体", 12);
label11.Font = new Font("宋体", 12);
label12.Font = new Font("宋体", 12);
label13.Font = new Font("宋体", 12);
label14.Font = new Font("宋体", 12);
label10.Font = new Font("宋体", 26, FontStyle.Bold);
*/ PaintEventArgs pe = new PaintEventArgs(panel1.CreateGraphics(), panel1.ClientRectangle);
DrawLine(pe);
} public void DrawLine(PaintEventArgs e)
{
/*
//画边框
Rectangle rc = e.ClipRectangle;
rc.Width = rc.Width - 1;
rc.Height = rc.Height - 1;
e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red)), rc);
*/ barcode.width = Convert.ToSingle(Mwidth.Text);
Graphics gh = e.Graphics;
if (dataToEncode.Text != "")
{
inputString = barcode.getCodeB(dataToEncode.Text);
//drawCodePic(); //把之前显示的用白色覆盖掉
//gh.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point(0, 0), new Size(413, 40)));
gh.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(, ), new Size(, ))); Brush br = new SolidBrush(Color.Black);
Pen p = new Pen(br, barcode.width);
Brush br1 = new SolidBrush(Color.White);
Pen p1 = new Pen(br1, barcode.width);
inputString = inputString.Trim();
float x = ;//这里是距离left //这里想把条码置中,已经画好了,怎么移动位置呢?
//先计算条码的width
float w = ;
for (int i = ; i < inputString.Length; i++)
{
for (int j = ; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % == )
{
w = w + barcode.width;
}
else
{
w = w + barcode.width;
}
}
}
x = (panel1.Width - (int)w) / ; for (int i = ; i < inputString.Length; i++)
{
for (int j = ; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % == )
{
//这里的2是距离top,30是条码高
gh.DrawLine(p, x, , x, );
x = x + barcode.width;
}
else
{
gh.DrawLine(p1, x, , x, );
x = x + barcode.width;
}
}
}
} if (textBox1.Text != "")
{
inputString = barcode.getCodeB(textBox1.Text);
//drawCodePic2(); //把之前显示的用白色覆盖掉
//gh.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point(0, 0), new Size(413, 40)));
gh.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(, ), new Size(, ))); e.Graphics.DrawString("P/N :" + "", new Font(new FontFamily("宋体"), ), System.Drawing.Brushes.Black, , );
e.Graphics.DrawString("单位:" + "EA", new Font(new FontFamily("宋体"), ), System.Drawing.Brushes.Black, , );
e.Graphics.DrawString("库位:" + "CMA110GV", new Font(new FontFamily("宋体"), ), System.Drawing.Brushes.Black, , );
e.Graphics.DrawString("", new Font(new FontFamily("宋体"), , FontStyle.Bold), System.Drawing.Brushes.Black, , );
e.Graphics.DrawString("数量:" + textBox1.Text, new Font(new FontFamily("宋体"), ), System.Drawing.Brushes.Black, , );
e.Graphics.DrawString("保质期:" + "" + "月", new Font(new FontFamily("宋体"), ), System.Drawing.Brushes.Black, , ); //画边框
gh.DrawRectangle(new Pen(new SolidBrush(Color.Black)), , , , ); e.Graphics.DrawString("CML", new Font(new FontFamily("宋体"), ), System.Drawing.Brushes.Black, , );
e.Graphics.DrawString("简单检查合格", new Font(new FontFamily("宋体"), ), System.Drawing.Brushes.Black, , );
e.Graphics.DrawString("IQC01", new Font(new FontFamily("宋体"), ), System.Drawing.Brushes.Black, , ); e.Graphics.DrawString(dataToEncode.Text, new Font(new FontFamily("宋体"), ), System.Drawing.Brushes.Black, (panel1.Width - dataToEncode.Text.Length * ) / , );
e.Graphics.DrawString("描述:" + "LAMINATIONJIS C2552-50A800", new Font(new FontFamily("宋体"), ), System.Drawing.Brushes.Black, , ); Brush br = new SolidBrush(Color.Black);
Pen p = new Pen(br, barcode.width);
Brush br1 = new SolidBrush(Color.White);
Pen p1 = new Pen(br1, barcode.width);
inputString = inputString.Trim();
float x = ;//这里是距离left for (int i = ; i < inputString.Length; i++)
{
for (int j = ; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % == )
{
//这里的2是距离top,30是条码高
gh.DrawLine(p, x, , x, );
x = x + barcode.width;
}
else
{
gh.DrawLine(p1, x, , x, );
x = x + barcode.width;
}
}
}
gh.Dispose();
}
} //预览
private void button3_Click(object sender, EventArgs e)
{
//保存为图片
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, new Rectangle(, , bmp.Width, bmp.Height));
bmp.Save(AppDomain.CurrentDomain.BaseDirectory + "1.png", System.Drawing.Imaging.ImageFormat.Jpeg); //预览
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.WindowState = FormWindowState.Maximized;
this.printPreviewDialog1.ShowDialog();
} private void button1_Click(object sender, EventArgs e)
{
PaintEventArgs pe = new PaintEventArgs(panel1.CreateGraphics(), panel1.ClientRectangle);
DrawLine(pe);
} private void panel1_Paint(object sender, PaintEventArgs e)
{
DrawLine(e); /*
//其他什么地方都不写,就写这里也可以
//画边框
Rectangle rc = e.ClipRectangle;
rc.Width = rc.Width - 1;
rc.Height = rc.Height - 1;
e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red)), rc); label11.Text = "数量:" + textBox1.Text;
barcode.width = Convert.ToSingle(Mwidth.Text);
Graphics gh = e.Graphics;
if (dataToEncode.Text != "")
{
inputString = barcode.getCodeB(dataToEncode.Text);
//drawCodePic(); //把之前显示的用白色覆盖掉
//gh.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point(0, 0), new Size(413, 40)));
gh.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(1, 70), new Size(411, 38))); Brush br = new SolidBrush(Color.Black);
Pen p = new Pen(br, barcode.width);
Brush br1 = new SolidBrush(Color.White);
Pen p1 = new Pen(br1, barcode.width);
inputString = inputString.Trim();
float x = 10;//这里是距离left //这里想把条码置中,已经画好了,怎么移动位置呢?
//先计算条码的width
float w = 0;
for (int i = 0; i < inputString.Length; i++)
{
for (int j = 0; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % 2 == 0)
{
w = w + barcode.width;
}
else
{
w = w + barcode.width;
}
}
}
x = (panel1.Width - (int)w) / 2; for (int i = 0; i < inputString.Length; i++)
{
for (int j = 0; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % 2 == 0)
{
//这里的2是距离top,30是条码高
gh.DrawLine(p, x, 70, x, 90);
x = x + barcode.width;
}
else
{
gh.DrawLine(p1, x, 70, x, 90);
x = x + barcode.width;
}
}
}
} if (textBox1.Text != "")
{
inputString = barcode.getCodeB(textBox1.Text);
//drawCodePic2(); //把之前显示的用白色覆盖掉
//gh.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point(0, 0), new Size(413, 40)));
gh.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(1, 26), new Size(411, 38))); Brush br = new SolidBrush(Color.Black);
Pen p = new Pen(br, barcode.width);
Brush br1 = new SolidBrush(Color.White);
Pen p1 = new Pen(br1, barcode.width);
inputString = inputString.Trim();
float x = 214;//这里是距离left for (int i = 0; i < inputString.Length; i++)
{
for (int j = 0; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % 2 == 0)
{
//这里的2是距离top,30是条码高
gh.DrawLine(p, x, 26, x, 46);
x = x + barcode.width;
}
else
{
gh.DrawLine(p1, x, 26, x, 46);
x = x + barcode.width;
}
}
}
gh.Dispose();
}
*/
} public void drawCodePic2()
{
//把之前显示的用白色覆盖掉
Graphics gh = panel1.CreateGraphics();//这种写法条码预览或打印都出不来,NND
//gh.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point(0, 0), new Size(413, 40)));
gh.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(, ), new Size(, ))); Brush br = new SolidBrush(Color.Black);
Pen p = new Pen(br, barcode.width);
Brush br1 = new SolidBrush(Color.White);
Pen p1 = new Pen(br1, barcode.width);
Graphics graphics = panel1.CreateGraphics();//这种写法条码预览或打印都出不来,NND
inputString = inputString.Trim();
float x = ;//这里是距离left
for (int i = ; i < inputString.Length; i++)
{
for (int j = ; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % == )
{
//这里的2是距离top,30是条码高
graphics.DrawLine(p, x, , x, );
x = x + barcode.width;
}
else
{
graphics.DrawLine(p1, x, , x, );
x = x + barcode.width;
}
}
}
graphics.Dispose();
} public void drawCodePic()
{
//把之前显示的用白色覆盖掉
Graphics gh = panel1.CreateGraphics();//这种写法条码预览或打印都出不来,NND
//gh.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point(0, 0), new Size(413, 40)));
gh.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(, ), new Size(, ))); Brush br = new SolidBrush(Color.Black);
Pen p = new Pen(br, barcode.width);
Brush br1 = new SolidBrush(Color.White);
Pen p1 = new Pen(br1, barcode.width);
Graphics graphics = panel1.CreateGraphics();
inputString = inputString.Trim();
float x = ;//这里是距离left //这里想把条码置中,已经画好了,怎么移动位置呢?
//先计算条码的width
float w = ;
for (int i = ; i < inputString.Length; i++)
{
for (int j = ; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % == )
{
w = w + barcode.width;
}
else
{
w = w + barcode.width;
}
}
}
x = (panel1.Width - (int)w) / ; for (int i = ; i < inputString.Length; i++)
{
for (int j = ; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % == )
{
//这里的2是距离top,30是条码高
graphics.DrawLine(p, x, , x, );
x = x + barcode.width;
}
else
{
graphics.DrawLine(p1, x, , x, );
x = x + barcode.width;
}
}
}
graphics.Dispose();
} private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//打印内容 为panel1
Bitmap _NewBitmap = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(_NewBitmap, new Rectangle(, , _NewBitmap.Width, _NewBitmap.Height));
e.Graphics.DrawImage(_NewBitmap, , , _NewBitmap.Width, _NewBitmap.Height);
} private void button2_Click(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false; //打印次数
printDocument1.PrinterSettings.Copies = ;// Convert.ToInt16(txt_print.Text.Trim()); this.printDocument1.Print();
/*
//换成多线程方式
if (thre == null)
{
thre = new Thread(this.printDocument1.Print);
thre.Start();
}
if (thre.ThreadState == ThreadState.Stopped)
{
thre.Abort();
GC.Collect();
thre = new Thread(this.printDocument1.Print);
thre.Start();
}
*/
} private void button4_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.ShowDialog();
}
}
}
DEMO3 相关类库
请参考类库ZXing.Net, 该类库也能实现二维码的生成,使用起来较为方便,而且开源。
jQuery生成二维条形码 jquery.qrcode.js
参考文章
C# 一维码生成的更多相关文章
- 一维码生成 c# winform GUI
最近看到同事小红在做一维码,感觉挺好玩,于是就在网上找了一个例子来玩玩. 下面的代码均为网上的代码,做了一些整理,但是忘记了出处,原作者看到可以提醒我,谢谢. 首先,一维码的相关知识可以先百度一下:h ...
- 使用Zxing 一维码
最近看到满大街的二维码扫码有惊喜,对二维码也有过一些了解,想看看到底是什么原理,在网上找了一些资料,自己弄了一个实例,采用的是MVC,贴出来分享一下 一维码生成 Controller public A ...
- Android生成一维码
BitmapUtil.java里面添加个方法 /** * 用于将给定的内容生成成一维码 注:目前生成内容为中文的话将直接报错,要修改底层jar包的内容 * * @param content 将要生成一 ...
- C# 使用ZXing.NET生成一维码、二维码
以上图片是本示例中的实际运行效果,在生活中我们的一维码(也就是条形码).二维码 使用已经非常广泛,那么如何使用c#.net来进行生成一维码(条形码).二维码呢? 使用ZXing来生成是非常方便的选择, ...
- zxing解析生成一维码二维码
@web界面实现扫一扫 二维码工具类 package util; import java.awt.BasicStroke; import java.awt.Graphics; import java. ...
- android二维码生成
前生: 一维码:条形码 数字 缺点:不好看,占面积, 好了,请看效果图: 在准备之前我们要导一个包:core-3.2.1.jar 下载请访问: http://download.csdn.net/do ...
- zxing 一维码部分深入分析与实际应用,识别卡片数量,Android数卡器
打算修改zxing 源码应用到其它方面,所以最近花了点时间阅读其源码,无意中找到这篇博客,条码扫描二维码扫描——ZXing android 简化源码分析 对过程的分析还是可以参考的.原作者给出的一个基 ...
- java利用zxing编码解码一维码与二维码
最近琢磨了一下二维码.一维码的编码.解码方法,感觉google的zxing用起来还是比较方便. 本人原创,欢迎转载,转载请标注原文地址:http://wallimn.iteye.com/blog/20 ...
- 【PYTHON】二维码生成
二维码是什么? 二维码从一维码扩展而来,增加另一维具有可读性的条码,用黑白矩形图形表示二进制数据,被设备扫描后获取其中包含的信息,二维码的长度.宽度均记载着数据,二维码具有定位点和容错机制,即便没有辨 ...
随机推荐
- Xcode 8 修改项目名
很麻烦,不想修改了 有个教程,贴一下. From: 大发写字的地方 Xcode8 修改包名(含cocopods)
- confd与etcd的使用
Add keys This guide assumes you have a working etcd, or consul server up and running and the ability ...
- CF1117C Magic Ship
CF1117C Magic Ship 考虑到答案具单调性(若第 \(i\) 天能到达目的点,第 \(i+1\) 天只需向风向相反的方向航行),可以二分答案. 现在要考虑给出一个天数 \(m\) ,问 ...
- POJ1201 Intervals【差分约束系统】
Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...
- NullReferenceException,就不应该存在!
如果要你说出 .NET 中的三个异常,NullReferenceException 一定会成为其中一个:如果说出 .NET 中的一个异常,NullReferenceException 也会被大多数人说 ...
- 关于RAS加密中pfx格式提取字符串私钥 (转)
openssl 使用以下命令 如果如果执行出错,请查看是否安装环境或在命令前面加上openssl --提取出来的无法直接使用 pkcs12 -in zhaoshang2.pfx -nocerts -n ...
- CentOS yum源设定使用方法的整理(转)
CentOS yum更新了很多版本更新,我本人认为CentOS yum很好使的文件系统,在此向大家推荐CentOS应该是做为服务器的linux的佼佼者.CentOS采用的二进制包是rpm,不过包的依赖 ...
- java的日期
直接看例子: import java.text.DateFormatSymbols; import java.util.Calendar; import java.util.GregorianCale ...
- mysql存储引擎之innodb学习
innodb引擎特点1.支持事务:支持4个事务隔离级别,支持多版本读. 2.行级锁定(更新时一般是锁定当前行):通过索引实现,全表扫描仍然会是表锁,注意间隙 锁的影响 3.读写阻塞与事务隔离级别有关 ...
- 系统管理员都要知道的 30 个 Linux 系统监控工具
1. top - 进程活动监控命令 top 命令会显示 Linux 的进程.它提供了一个运行中系统的实时动态视图,即实际的进程活动.默认情况下,它显示在服务器上运行的 CPU 占用率最高的任务,并且每 ...