c# 生成条形码

简介:


from:http://blog.blueshop.com.tw/timothychi/articles/48836.aspx

using System.Drawing;

using System.Drawing.Imaging;

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;

}




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2010/04/15/1712676.html,如需转载请自行联系原作者

相关文章
|
3月前
|
算法 开发工具 计算机视觉
条形码识别研究
条形码识别研究
|
7月前
使用jsbarcode生成条形码
使用jsbarcode生成条形码
78 0
|
9月前
|
机器学习/深度学习 计算机视觉
定位和读取图像中的多个条形码
定位和读取图像中的多个条形码。
161 0
定位和读取图像中的多个条形码
|
10月前
防伪彩色二维码的设计制作
彩码指彩色二维码、一般使用QRcode类型,彩码的作用主要是防伪领域
86 0
|
10月前
|
数据库
导出矢量条码二维码的方法
码在设计印刷行业,经常使用导入到Coreldraw或Illustrator等矢量环境排版使用。矢量条码最大的优点是无论你将它放大、缩小或旋转等它都有一样平滑的边缘, 一样的清晰度,不会失真。
79 1
|
边缘计算 并行计算 算法
Opencv+ZBar识别条形码、二维码
Opencv+ZBar识别条形码、二维码
605 0
Opencv+ZBar识别条形码、二维码
制作条形码的手机App推荐
条形码自发明以来,给我们的生活带来极大便利,怎么才能快速的用手机创建条形码呢?下面就给大家介绍一款小编常用的制作条形码的App--二维码和条形码生成器。使用它,不仅可以制作条形码、制作二维码,还能快速扫描二维码和条形码,并且支持查看制作和扫描的历史,功能强大,关键还是免费的!
|
算法 前端开发
距离相等的条形码
🎈今天给大家带来的是算法练习,题目为"距离相等的条形码"。
80 0