栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

条形码在生活中的应用非常广泛,具体的条形码知识大家自行百度,了解条形码知识对理解下面的代码是必要的。如果只是应用的话,直接拿去就可以用了。
废话不多说,上代码
复制代码 代码如下:
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 =
             {
                 "000110100", 
                 "100100001", 
                 "001100001", 
                 "101100000",
                 "000110001", 
                 "100110000", 
                 "001110000", 
                 "000100101",
                 "100100100", 
                 "001100100", 
                 "100001001", 
                 "001001001",
                 "101001000", 
                 "000011001", 
                 "100011000", 
                 "001011000",
                 "000001101", 
                 "100001100", 
                 "001001100", 
                 "000011100",
                 "100000011", 
                 "001000011", 
                 "101000010", 
                 "000010011",
                 "100010010", 
                 "001010010", 
                 "000000111", 
                 "100000110",
                 "001000110", 
                 "000010110", 
                 "110000001", 
                 "011000001",
                 "111000000", 
                 "010010001", 
                 "110010000", 
                 "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 命名空间。

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/127376.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号