欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

BarCode条形码基于C# GDI+ 的实现方法详解

程序员文章站 2023-12-18 23:17:04
条形码在生活中的应用非常广泛,具体的条形码知识大家自行百度,了解条形码知识对理解下面的代码是必要的。如果只是应用的话,直接拿去就可以用了。废话不多说,上代码复制代码 代码如...

条形码在生活中的应用非常广泛,具体的条形码知识大家自行百度,了解条形码知识对理解下面的代码是必要的。如果只是应用的话,直接拿去就可以用了。
废话不多说,上代码

复制代码 代码如下:

public bitmap getcode39(string sourcecode)
         {
             int leftmargin = 5;
             int topmargin = 0;
             int thicklength = 2;
             int narrowlength = 1;
             int barcodeheight = 35;
             int intsourcelength = sourcecode.length;
             string strencode = "010010100"; //添加起始码“*”.
             var font = new system.drawing.font("segoe ui", 5);

             string alphabet = "0123456789abcdefghijklmnopqrstuvwxyz-. $/+%*";

             string[] 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" 
             };
             sourcecode = sourcecode.toupper();

             bitmap objbitmap = new bitmap(
               ((thicklength * 3 + narrowlength * 7) * (intsourcelength + 2)) + (leftmargin * 2),
               barcodeheight + (topmargin * 2));
             graphics objgraphics = graphics.fromimage(objbitmap);

             objgraphics.fillrectangle(brushes.white, 0, 0, objbitmap.width, objbitmap.height);

             for (int i = 0; i < intsourcelength; i++)
             {
                 //非法字符校验
                 if (alphabet.indexof(sourcecode[i]) == -1 || 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 = 0; i < intencodelength; i++) //绘制 code39 barcode
             {
                 intbarwidth = strencode[i] == '1' ? thicklength : narrowlength;
                 objgraphics.fillrectangle(i % 2 == 0 ? brushes.black : brushes.white,
                  leftmargin, topmargin, intbarwidth, barcodeheight);
                 leftmargin += intbarwidth;
             }

             //绘制 明码
             sizef sizef = objgraphics.measurestring(sourcecode, font);
             float x=(objbitmap.width - sizef.width) / 2;
             float y = objbitmap.height - sizef.height;
             objgraphics.fillrectangle(brushes.white, x, y, sizef.width, sizef.height);
             objgraphics.drawstring(sourcecode, font, brushes.black, x, y);

             return objbitmap;
         }

新建一个winform程序拖一个picturebox控件,然后把方法返回的图片绑到picturebox上,就可以看到结果了。

以上代码需要引用 system.drawing 命名空间。

顺带提一句,还有一种最简单的办法就是下载条形码字体,然后输出的时候设定字体为条形码字体,显示的就是条形码了。不过你不能要求每个客户机都装这种字体。这个是个明显的缺陷,所以不推荐使用这种方法。

上一篇:

下一篇: