博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
生成 39 条形码
阅读量:6360 次
发布时间:2019-06-23

本文共 2799 字,大约阅读时间需要 9 分钟。

        private Bitmap GetCode39(string strSource)

        {
            int x = 5; //左邊界
            int y = 0; //上邊界
            int WidLength = 2; //粗BarCode長度
            int NarrowLength = 1; //細BarCode長度
            int BarCodeHeight = 50; //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)+12);

            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;
            }

            Graphics g = Graphics.FromImage(objBitmap);

            String str = strSource;
            Font font = new Font("宋体", 10);
            SolidBrush sbrush = new SolidBrush(Color.Black);
            g.DrawString(str, font, sbrush, 0, y + BarCodeHeight);

            return objBitmap;

        }

转载于:https://www.cnblogs.com/xiguanjiandan/p/3155122.html

你可能感兴趣的文章
提升HTTPS安全评级
查看>>
iOS开发过程中的心得
查看>>
QOS配置命令
查看>>
使用 MPI for Python 并行化遗传算法
查看>>
paramiko安装及使用
查看>>
我的友情链接
查看>>
《Python网络数据采集》读书笔记(六)
查看>>
Linux必学的60个命令
查看>>
iptables 学习笔记 (上)
查看>>
Windows Server 2012 R2 Active Directory(活动目录)实验一
查看>>
android viewpager 无限左右滑动
查看>>
linux下SSH远程连接服务慢解决方案
查看>>
利用mic visual studio 2010 编译器执行wincap获取网络适配器的代码
查看>>
HTML
查看>>
CENTOS7下编译安装PHP-5.4以及配置phpMyAdmin
查看>>
磁盘显示无法访问拒绝访问,里面的资料怎样找到
查看>>
Java之品优购课程讲义_day07(5)
查看>>
Java的新项目学成在线笔记-day3(八)
查看>>
路由简单的实验
查看>>
零基础学习大数据Hadoop需要什么准备?Hadoop如何发展起来的?
查看>>