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】二维码生成
二维码是什么? 二维码从一维码扩展而来,增加另一维具有可读性的条码,用黑白矩形图形表示二进制数据,被设备扫描后获取其中包含的信息,二维码的长度.宽度均记载着数据,二维码具有定位点和容错机制,即便没有辨 ...
随机推荐
- POJ2891 Strange Way to Express Integers【扩展中国剩余定理】
题目大意 就是模板...没啥好说的 思路 因为模数不互质,所以直接中国剩余定理肯定是不对的 然后就考虑怎么合并两个同余方程 \(ans = a_1 + x_1 * m_1 = a_2 + x_2 * ...
- BZOJ2216 Poi2011 Lightning Conductor 【决策单调性优化DP】
Description 已知一个长度为n的序列a1,a2,...,an. 对于每个1<=i<=n,找到最小的非负整数p满足 对于任意的j, aj < = ai + p - sqrt( ...
- .NET 中 GetProcess 相关方法的性能
.NET 的 Process 类中提供了查找进程的若干方法,其中部分方法还比较消耗性能.如果你试图优化查找进程相关方法的性能,可能本文分享的一些耗时数据可以作为参考. 性能比较 Process 类 ...
- 如何快速编写和调试 Emit 生成 IL 的代码
.NET Core/.NET Framework 的 System.Reflection.Emit 命名空间为我们提供了动态生成 IL 代码的能力.利用这项能力,我们能够在运行时生成一段代码/一个方法 ...
- 错误:Authentication with old password no longer supported, use 4.1 style passwords.
今天重新装了系统,mysql connector 使用了官网的最新版本(6.8.3). 启动项目出现标题中的错误.原因大家谷歌. 解决方法,使用低版本的 connector. 我使用的是 6.5.6 ...
- FileStream读写文件
读文件示例 try { // 打开文件 FileStream fs = new FileStream("D:\\not.txt", FileMode.Open, FileAcces ...
- LuaJavaBridge
http://www.360doc.com/content/14/0117/13/9200790_345940368.shtml quick目录结构介绍 http://cn.cocos2d-x.org ...
- [LeetCode系列]翻转链表问题II
给定一个链表和两个整数m, n, 翻转链表第m个节点到第n个节点(从1开始计数). 如, 给定链表: 1->2->3->4->5->NULL, 以及 m = 2, n = ...
- oracle表空间维护常用命令
---查看表空间的名字及文件所在位置: select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_spac ...
- Vmvare + Ubuntu 16.04环境搭建 + 相关软件安装配置笔记【深度学习】
前言 由于学习与工作的需要,加上之前配置好的vmmachines都损坏了,我就重新弄一个ubuntu虚拟机,配置一下环境,给自己留个记录 1.文件 2.配置过程 1.在Vmware中新建虚拟机,自定义 ...