-博客很久沒有更新了,最近在做倉儲系統的東西,所以用到了一維碼。在參考了其他人的一些解決方案后加上自己的改良,便有了下面的方法。經過掃描機測試完全可用,跟大家分享一下。
條形碼在生活中的應用非常廣泛,具體的條形碼知識大家自行百度,了解條形碼知識對理解下面的代碼是必要的。如果只是應用的話,直接拿去就可以用了。廢話不多說,上代碼
1 public Bitmap GetCode39(string sourceCode) 2 { 3 int leftMargin = 5; 4 int topMargin = 0; 5 int thickLength = 2; 6 int narrowLength = 1; 7 int barCodeHeight = 35; 8 int intSourceLength = sourceCode.Length; 9 string strEncode = "010010100"; //添加起始碼“*”. 10 var font = new System.Drawing.Font("Segoe UI", 5); 11 12 string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; 13 14 string[] Code39 = 15 { 16 /* 0 */ "000110100", 17 /* 1 */ "100100001", 18 /* 2 */ "001100001", 19 /* 3 */ "101100000", 20 /* 4 */ "000110001", 21 /* 5 */ "100110000", 22 /* 6 */ "001110000", 23 /* 7 */ "000100101", 24 /* 8 */ "100100100", 25 /* 9 */ "001100100", 26 /* A */ "100001001", 27 /* B */ "001001001", 28 /* C */ "101001000", 29 /* D */ "000011001", 30 /* E */ "100011000", 31 /* F */ "001011000", 32 /* G */ "000001101", 33 /* H */ "100001100", 34 /* I */ "001001100", 35 /* J */ "000011100", 36 /* K */ "100000011", 37 /* L */ "001000011", 38 /* M */ "101000010", 39 /* N */ "000010011", 40 /* O */ "100010010", 41 /* P */ "001010010", 42 /* Q */ "000000111", 43 /* R */ "100000110", 44 /* S */ "001000110", 45 /* T */ "000010110", 46 /* U */ "110000001", 47 /* V */ "011000001", 48 /* W */ "111000000", 49 /* X */ "010010001", 50 /* Y */ "110010000", 51 /* Z */ "011010000", 52 /* - */ "010000101", 53 /* . */ "110000100", 54 /*' '*/ "011000100", 55 /* $ */ "010101000", 56 /* / */ "010100010", 57 /* + */ "010001010", 58 /* % */ "000101010", 59 /* * */ "010010100" 60 }; 61 sourceCode = sourceCode.ToUpper(); 62 63 Bitmap objBitmap = new Bitmap( 64 ((thickLength * 3 + narrowLength * 7) * (intSourceLength + 2)) + (leftMargin * 2), 65 barCodeHeight + (topMargin * 2)); 66 Graphics objGraphics = Graphics.FromImage(objBitmap); 67 68 objGraphics.FillRectangle(Brushes.White, 0, 0, objBitmap.Width, objBitmap.Height); 69 70 for (int i = 0; i < intSourceLength; i++) 71 { 72 //非法字符校驗 73 if (AlphaBet.IndexOf(sourceCode[i]) == -1 || sourceCode[i] == '*') 74 { 75 objGraphics.DrawString("Invalid Bar Code", 76 SystemFonts.DefaultFont, Brushes.Red, leftMargin, topMargin); 77 return objBitmap; 78 } 79 //編碼 80 strEncode = string.Format("{0}0{1}", strEncode, 81 Code39[AlphaBet.IndexOf(sourceCode[i])]); 82 } 83 84 strEncode = string.Format("{0}0010010100", strEncode); //添加結束碼“*” 85 86 int intEncodeLength = strEncode.Length; 87 int intBarWidth; 88 89 for (int i = 0; i < intEncodeLength; i++) //繪制 Code39 barcode 90 { 91 intBarWidth = strEncode[i] == '1' ? thickLength : narrowLength; 92 objGraphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White, 93 leftMargin, topMargin, intBarWidth, barCodeHeight); 94 leftMargin += intBarWidth; 95 } 96 97 //繪制 明碼 98 SizeF sizeF = objGraphics.MeasureString(sourceCode, font); 99 float x=(objBitmap.Width - sizeF.Width) / 2; 100 float y = objBitmap.Height - sizeF.Height; 101 objGraphics.FillRectangle(Brushes.White, x, y, sizeF.Width, sizeF.Height); 102 objGraphics.DrawString(sourceCode, font, Brushes.Black, x, y); 103 104 return objBitmap; 105 }
新建一個Winform程序拖一個PictureBox控件,然后把方法返回的圖片綁到PictureBox上,就可以看到結果了。
這里明碼是www.cnblogs.com
以上代碼需要引用 System.Drawing 命名空間。
順帶提一句,還有一種最簡單的辦法就是下載條形碼字體,然后輸出的時候設定字體為條形碼字體,顯示的就是條形碼了。不過你不能要求每個客戶機都裝這種字體。這個是個明顯的缺陷,所以不推薦使用這種方法。
感謝樓下DataCool的封裝,經他封裝后的代碼在這里:http://files.cnblogs.com/datacool/Code39BarCode.zip
大家可以試着添加其他類型的條碼然后跟我們分享,最終可能會變成一個組件,這是后話。