国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

BarCode條形碼基于C# GDI+ 的實(shí)現(xiàn)方法詳解

2020-01-24 03:23:36
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

條形碼在生活中的應(yīng)用非常廣泛,具體的條形碼知識(shí)大家自行百度,了解條形碼知識(shí)對(duì)理解下面的代碼是必要的。如果只是應(yīng)用的話,直接拿去就可以用了。
廢話不多說(shuō),上代碼

復(fù)制代碼 代碼如下:

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++)
             {
                 //非法字符校驗(yàn)
                 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); //添加結(jié)束碼“*”

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

新建一個(gè)Winform程序拖一個(gè)PictureBox控件,然后把方法返回的圖片綁到PictureBox上,就可以看到結(jié)果了。

以上代碼需要引用 System.Drawing 命名空間。

順帶提一句,還有一種最簡(jiǎn)單的辦法就是下載條形碼字體,然后輸出的時(shí)候設(shè)定字體為條形碼字體,顯示的就是條形碼了。不過(guò)你不能要求每個(gè)客戶機(jī)都裝這種字體。這個(gè)是個(gè)明顯的缺陷,所以不推薦使用這種方法。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 吐鲁番市| 遵义市| 玛纳斯县| 海门市| 环江| 江西省| 忻州市| 平陆县| 永年县| 巧家县| 孟连| 泰安市| 台东县| 濮阳市| 鹤庆县| 治多县| 绥化市| 武强县| 安福县| 神池县| 紫金县| 公主岭市| 安远县| 苏尼特右旗| 理塘县| 兖州市| 天津市| 盐城市| 黑水县| 白银市| 如皋市| 阿拉善右旗| 五河县| 宁远县| 登封市| 揭东县| 疏附县| 界首市| 韶关市| 柳州市| 龙胜|