【code128条形码组成】
除终止符(STOP)由13个模块组成外,其他字符均由11个模块组成
就是说,如果用‘1’表示黑线(实模块),用‘0’表示白线(空模块),那么每表示一个字符就需要11条线,也就是11个模块。
在条码字符中,每三个条和三个空组成一个字符,终止符有四个条和三个空组成。
就是说,这11条线的颜色,是依据一定的规则将其分布于三个黑线区和三个白线区,当然是黑白相间的。就像这样:
11100110100,11011011000。具体的编码要参照码表,查找对应的字符或编码。
【code128码表】其中b=>black代表黑线,s=>space代表白线
ID | Code128A | Code128B | Code128C | BandCode | 编码值 |
0 | SP | SP | 0 | 212222 | bbsbbssbbss |
1 | ! | ! | 1 | 222122 | bbssbbsbbss |
2 | " | " | 2 | 222221 | bbssbbssbbs |
3 | # | # | 3 | 121223 | bssbssbbsss |
4 | $ | $ | 4 | 121322 | bssbsssbbss |
5 | % | % | 5 | 131222 | bsssbssbbss |
6 | & | & | 6 | 122213 | bssbbssbsss |
7 | ' | ' | 7 | 122312 | bssbbsssbss |
8 | ( | ( | 8 | 132212 | bsssbbssbss |
9 | ) | ) | 9 | 221213 | bbssbssbsss |
10 | * | * | 10 | 221312 | bbssbsssbss |
11 | + | + | 11 | 231212 | bbsssbssbss |
12 | , | , | 12 | 112232 | bsbbssbbbss |
13 | - | - | 13 | 122132 | bssbbsbbbss |
14 | . | . | 14 | 122231 | bssbbssbbbs |
15 | / | / | 15 | 113222 | bsbbbssbbss |
16 | 0 | 0 | 16 | 123122 | bssbbbsbbss |
17 | 1 | 1 | 17 | 123221 | bssbbbssbbs |
18 | 2 | 2 | 18 | 223211 | bbssbbbssbs |
19 | 3 | 3 | 19 | 221132 | bbssbsbbbss |
20 | 4 | 4 | 20 | 221231 | bbssbssbbbs |
21 | 5 | 5 | 21 | 213212 | bbsbbbssbss |
22 | 6 | 6 | 22 | 223112 | bbssbbbsbss |
23 | 7 | 7 | 23 | 312131 | bbbsbbsbbbs |
24 | 8 | 8 | 24 | 311222 | bbbsbssbbss |
25 | 9 | 9 | 25 | 321122 | bbbssbsbbss |
26 | : | : | 26 | 321221 | bbbssbssbbs |
27 | ; | ; | 27 | 312212 | bbbsbbssbss |
28 | < | < | 28 | 322112 | bbbssbbsbss |
29 | = | = | 29 | 322211 | bbbssbbssbs |
30 | > | > | 30 | 212123 | bbsbbsbbsss |
31 | ? | ? | 31 | 212321 | bbsbbsssbbs |
32 | @ | @ | 32 | 232121 | bbsssbbsbbs |
33 | A | A | 33 | 111323 | bsbsssbbsss |
34 | B | B | 34 | 131123 | bsssbsbbsss |
35 | C | C | 35 | 131321 | bsssbsssbbs |
36 | D | D | 36 | 112313 | bsbbsssbsss |
37 | E | E | 37 | 132113 | bsssbbsbsss |
38 | F | F | 38 | 132311 | bsssbbsssbs |
39 | G | G | 39 | 211313 | bbsbsssbsss |
40 | H | H | 40 | 231113 | bbsssbsbsss |
41 | I | I | 41 | 231311 | bbsssbsssbs |
42 | J | J | 42 | 112133 | bsbbsbbbsss |
43 | K | K | 43 | 112331 | bsbbsssbbbs |
44 | L | L | 44 | 132131 | bsssbbsbbbs |
45 | M | M | 45 | 113123 | bsbbbsbbsss |
46 | N | N | 46 | 113321 | bsbbbsssbbs |
47 | O | O | 47 | 133121 | bsssbbbsbbs |
48 | P | P | 48 | 313121 | bbbsbbbsbbs |
49 | Q | Q | 49 | 211331 | bbsbsssbbbs |
50 | R | R | 50 | 231131 | bbsssbsbbbs |
51 | S | S | 51 | 213113 | bbsbbbsbsss |
52 | T | T | 52 | 213311 | bbsbbbsssbs |
53 | U | U | 53 | 213131 | bbsbbbsbbbs |
54 | V | V | 54 | 311123 | bbbsbsbbsss |
55 | W | W | 55 | 311321 | bbbsbsssbbs |
56 | X | X | 56 | 331121 | bbbsssbsbbs |
57 | Y | Y | 57 | 312113 | bbbsbbsbsss |
58 | Z | Z | 58 | 312311 | bbbsbbsssbs |
59 | [ | [ | 59 | 332111 | bbbsssbbsbs |
60 | \ | \ | 60 | 314111 | bbbsbbbbsbs |
61 | ] | ] | 61 | 221411 | bbssbssssbs |
62 | ^ | ^ | 62 | 431111 | bbbbsssbsbs |
63 | _ | _ | 63 | 111224 | bsbssbbssss |
64 | NUL | ` | 64 | 111422 | bsbssssbbss |
65 | SOH | a | 65 | 121124 | bssbsbbssss |
66 | STX | b | 66 | 121421 | bssbssssbbs |
67 | ETX | c | 67 | 141122 | bssssbsbbss |
68 | EOT | d | 68 | 141221 | bssssbssbbs |
69 | ENQ | e | 69 | 112214 | bsbbssbssss |
70 | ACK | f | 70 | 112412 | bsbbssssbss |
71 | BEL | g | 71 | 122114 | bssbbsbssss |
72 | BS | h | 72 | 122411 | bssbbssssbs |
73 | HT | i | 73 | 142112 | bssssbbsbss |
74 | LF | j | 74 | 142211 | bssssbbssbs |
75 | VT | k | 75 | 241211 | bbssssbssbs |
76 | FF | I | 76 | 221114 | bbssbsbssss |
77 | CR | m | 77 | 413111 | bbbbsbbbsbs |
78 | SO | n | 78 | 241112 | bbssssbsbss |
79 | SI | o | 79 | 134111 | bsssbbbbsbs |
80 | DLE | p | 80 | 111242 | bsbssbbbbss |
81 | DC1 | q | 81 | 121142 | bssbsbbbbss |
82 | DC2 | r | 82 | 121241 | bssbssbbbbs |
83 | DC3 | s | 83 | 114212 | bsbbbbssbss |
84 | DC4 | t | 84 | 124112 | bssbbbbsbss |
85 | NAK | u | 85 | 124211 | bssbbbbssbs |
86 | SYN | v | 86 | 411212 | bbbbsbssbss |
87 | ETB | w | 87 | 421112 | bbbbssbsbss |
88 | CAN | x | 88 | 421211 | bbbbssbssbs |
89 | EM | y | 89 | 212141 | bbsbbsbbbbs |
90 | SUB | z | 90 | 214121 | bbsbbbbsbbs |
91 | ESC | { | 91 | 412121 | bbbbsbbsbbs |
92 | FS | | | 92 | 111143 | bsbsbbbbsss |
93 | GS | } | 93 | 111341 | bsbsssbbbbs |
94 | RS | ~ | 94 | 131141 | bsssbsbbbbs |
95 | US | DEL | 95 | 114113 | bsbbbbsbsss |
96 | FNC3 | FNC3 | 96 | 114311 | bsbbbbsssbs |
97 | FNC2 | FNC2 | 97 | 411113 | bbbbsbsbsss |
98 | SHIFT | SHIFT | 98 | 411311 | bbbbsbsssbs |
99 | CODEC | CODEC | 99 | 113141 | bsbbbsbbbbs |
100 | CODEB | FNC4 | CODEB | 114131 | bsbbbbsbbbs |
101 | FNC4 | CODEA | CODEA | 311141 | bbbsbsbbbbs |
102 | FNC1 | FNC1 | FNC1 | 411131 | bbbbsbsbbbs |
103 | StartA | StartA | StartA | 211412 | bbsbssssbss |
104 | StartB | StartB | StartB | 211214 | bbsbssbssss |
105 | StartC | StartC | StartC | 211232 | bbsbssbbbss |
106 | Stop | Stop | Stop | 2331112 | bbsssbbbsbsbb |
【Code128各编码方式的编码范围】
1、Code128A:标准数字和字母,控制符,特殊字符;
2、Code128B:标准数字和字母,小写字母,特殊字符;
3、Code128C/EAN128:[00]-[99]的数字对集合,共100个,即只能表示偶数位长度的数字。
【Code128编码规则】:开始位 + [FNC1(为EAN128码时加)] + 数据位 + 检验位 + 结束位
【Code128检验位计算】:(开始位对应的ID值 + 每位数据在整个数据中的位置×每位数据对应的ID值)% 103
编码类型 | 开始位 | FNC1 |
Code128A | StartA | 无 |
Code128B | StartB | 无 |
Code128C | StartC | 无 |
Ean128 | StartC | FNC1 |
【C#代码】
1 using System; 2 using System.Data; 3 using System.Collections.Generic; 4 using System.Text; 5 using System.Drawing; 6 7 8 namespace XXXX.Common 9 { 10 /// <summary> 11 /// 生成Code128A,Code128B,Code128C,EAN128条码 12 /// </summary> 13 public class BarCode 14 { 15 /// <summary> 16 /// 生成条形码 17 /// </summary> 18 /// <param name="barNumber">条形码,位数必须为2的倍数</param> 19 public void CreateBarCode(string barNumber) 20 { 21 if (barNumber.Length % 2 == 1) barNumber = "0" + barNumber; 22 Code128 code = new Code128(); 23 //code.ValueFont = new Font("Arial", 9); //显示条码文字字体 24 code.Height = 38; 25 Bitmap image = code.GetCodeImage(barNumber, Common.BarCode.Code128.Encode.Code128C); 26 image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); 27 } 28 public class Code128 29 { 30 private DataTable m_Code128 = new DataTable(); 31 private uint m_Height = 40; 32 /// <summary> 33 /// 高度 34 /// </summary> 35 public uint Height { get { return m_Height; } set { m_Height = value; } } 36 private Font m_ValueFont = null; 37 /// <summary> 38 /// 是否显示可见号码 如果为NULL不显示号码 39 /// </summary> 40 public Font ValueFont { get { return m_ValueFont; } set { m_ValueFont = value; } } 41 private byte m_Magnify = 0; 42 /// <summary> 43 /// 放大倍数 44 /// </summary> 45 public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } } 46 /// <summary> 47 /// 条码类别 48 /// </summary> 49 public enum Encode 50 { 51 Code128A, 52 Code128B, 53 Code128C, 54 EAN128 55 } 56 /// <summary> 57 /// 数据表 58 /// </summary> 59 public Code128() 60 { 61 m_Code128.Columns.Add("ID"); 62 m_Code128.Columns.Add("Code128A"); 63 m_Code128.Columns.Add("Code128B"); 64 m_Code128.Columns.Add("Code128C"); 65 m_Code128.Columns.Add("BandCode"); 66 m_Code128.CaseSensitive = true; 67 #region 数据表 68 m_Code128.Rows.Add("0", " ", " ", "00", "212222"); 69 m_Code128.Rows.Add("1", "!", "!", "01", "222122"); 70 m_Code128.Rows.Add("2", "\"", "\"", "02", "222221"); 71 m_Code128.Rows.Add("3", "#", "#", "03", "121223"); 72 m_Code128.Rows.Add("4", "$", "$", "04", "121322"); 73 m_Code128.Rows.Add("5", "%", "%", "05", "131222"); 74 m_Code128.Rows.Add("6", "&", "&", "06", "122213"); 75 m_Code128.Rows.Add("7", "'", "'", "07", "122312"); 76 m_Code128.Rows.Add("8", "(", "(", "08", "132212"); 77 m_Code128.Rows.Add("9", ")", ")", "09", "221213"); 78 m_Code128.Rows.Add("10", "*", "*", "10", "221312"); 79 m_Code128.Rows.Add("11", "+", "+", "11", "231212"); 80 m_Code128.Rows.Add("12", ",", ",", "12", "112232"); 81 m_Code128.Rows.Add("13", "-", "-", "13", "122132"); 82 m_Code128.Rows.Add("14", ".", ".", "14", "122231"); 83 m_Code128.Rows.Add("15", "/", "/", "15", "113222"); 84 m_Code128.Rows.Add("16", "0", "0", "16", "123122"); 85 m_Code128.Rows.Add("17", "1", "1", "17", "123221"); 86 m_Code128.Rows.Add("18", "2", "2", "18", "223211"); 87 m_Code128.Rows.Add("19", "3", "3", "19", "221132"); 88 m_Code128.Rows.Add("20", "4", "4", "20", "221231"); 89 m_Code128.Rows.Add("21", "5", "5", "21", "213212"); 90 m_Code128.Rows.Add("22", "6", "6", "22", "223112"); 91 m_Code128.Rows.Add("23", "7", "7", "23", "312131"); 92 m_Code128.Rows.Add("24", "8", "8", "24", "311222"); 93 m_Code128.Rows.Add("25", "9", "9", "25", "321122"); 94 m_Code128.Rows.Add("26", ":", ":", "26", "321221"); 95 m_Code128.Rows.Add("27", ";", ";", "27", "312212"); 96 m_Code128.Rows.Add("28", "<", "<", "28", "322112"); 97 m_Code128.Rows.Add("29", "=", "=", "29", "322211"); 98 m_Code128.Rows.Add("30", ">", ">", "30", "212123"); 99 m_Code128.Rows.Add("31", "?", "?", "31", "212321"); 100 m_Code128.Rows.Add("32", "@", "@", "32", "232121"); 101 m_Code128.Rows.Add("33", "A", "A", "33", "111323"); 102 m_Code128.Rows.Add("34", "B", "B", "34", "131123"); 103 m_Code128.Rows.Add("35", "C", "C", "35", "131321"); 104 m_Code128.Rows.Add("36", "D", "D", "36", "112313"); 105 m_Code128.Rows.Add("37", "E", "E", "37", "132113"); 106 m_Code128.Rows.Add("38", "F", "F", "38", "132311"); 107 m_Code128.Rows.Add("39", "G", "G", "39", "211313"); 108 m_Code128.Rows.Add("40", "H", "H", "40", "231113"); 109 m_Code128.Rows.Add("41", "I", "I", "41", "231311"); 110 m_Code128.Rows.Add("42", "J", "J", "42", "112133"); 111 m_Code128.Rows.Add("43", "K", "K", "43", "112331"); 112 m_Code128.Rows.Add("44", "L", "L", "44", "132131"); 113 m_Code128.Rows.Add("45", "M", "M", "45", "113123"); 114 m_Code128.Rows.Add("46", "N", "N", "46", "113321"); 115 m_Code128.Rows.Add("47", "O", "O", "47", "133121"); 116 m_Code128.Rows.Add("48", "P", "P", "48", "313121"); 117 m_Code128.Rows.Add("49", "Q", "Q", "49", "211331"); 118 m_Code128.Rows.Add("50", "R", "R", "50", "231131"); 119 m_Code128.Rows.Add("51", "S", "S", "51", "213113"); 120 m_Code128.Rows.Add("52", "T", "T", "52", "213311"); 121 m_Code128.Rows.Add("53", "U", "U", "53", "213131"); 122 m_Code128.Rows.Add("54", "V", "V", "54", "311123"); 123 m_Code128.Rows.Add("55", "W", "W", "55", "311321"); 124 m_Code128.Rows.Add("56", "X", "X", "56", "331121"); 125 m_Code128.Rows.Add("57", "Y", "Y", "57", "312113"); 126 m_Code128.Rows.Add("58", "Z", "Z", "58", "312311"); 127 m_Code128.Rows.Add("59", "[", "[", "59", "332111"); 128 m_Code128.Rows.Add("60", "\\", "\\", "60", "314111"); 129 m_Code128.Rows.Add("61", "]", "]", "61", "221411"); 130 m_Code128.Rows.Add("62", "^", "^", "62", "431111"); 131 m_Code128.Rows.Add("63", "_", "_", "63", "111224"); 132 m_Code128.Rows.Add("64", "NUL", "`", "64", "111422"); 133 m_Code128.Rows.Add("65", "SOH", "a", "65", "121124"); 134 m_Code128.Rows.Add("66", "STX", "b", "66", "121421"); 135 m_Code128.Rows.Add("67", "ETX", "c", "67", "141122"); 136 m_Code128.Rows.Add("68", "EOT", "d", "68", "141221"); 137 m_Code128.Rows.Add("69", "ENQ", "e", "69", "112214"); 138 m_Code128.Rows.Add("70", "ACK", "f", "70", "112412"); 139 m_Code128.Rows.Add("71", "BEL", "g", "71", "122114"); 140 m_Code128.Rows.Add("72", "BS", "h", "72", "122411"); 141 m_Code128.Rows.Add("73", "HT", "i", "73", "142112"); 142 m_Code128.Rows.Add("74", "LF", "j", "74", "142211"); 143 m_Code128.Rows.Add("75", "VT", "k", "75", "241211"); 144 m_Code128.Rows.Add("76", "FF", "I", "76", "221114"); 145 m_Code128.Rows.Add("77", "CR", "m", "77", "413111"); 146 m_Code128.Rows.Add("78", "SO", "n", "78", "241112"); 147 m_Code128.Rows.Add("79", "SI", "o", "79", "134111"); 148 m_Code128.Rows.Add("80", "DLE", "p", "80", "111242"); 149 m_Code128.Rows.Add("81", "DC1", "q", "81", "121142"); 150 m_Code128.Rows.Add("82", "DC2", "r", "82", "121241"); 151 m_Code128.Rows.Add("83", "DC3", "s", "83", "114212"); 152 m_Code128.Rows.Add("84", "DC4", "t", "84", "124112"); 153 m_Code128.Rows.Add("85", "NAK", "u", "85", "124211"); 154 m_Code128.Rows.Add("86", "SYN", "v", "86", "411212"); 155 m_Code128.Rows.Add("87", "ETB", "w", "87", "421112"); 156 m_Code128.Rows.Add("88", "CAN", "x", "88", "421211"); 157 m_Code128.Rows.Add("89", "EM", "y", "89", "212141"); 158 m_Code128.Rows.Add("90", "SUB", "z", "90", "214121"); 159 m_Code128.Rows.Add("91", "ESC", "{", "91", "412121"); 160 m_Code128.Rows.Add("92", "FS", "|", "92", "111143"); 161 m_Code128.Rows.Add("93", "GS", "}", "93", "111341"); 162 m_Code128.Rows.Add("94", "RS", "~", "94", "131141"); 163 m_Code128.Rows.Add("95", "US", "DEL", "95", "114113"); 164 m_Code128.Rows.Add("96", "FNC3", "FNC3", "96", "114311"); 165 m_Code128.Rows.Add("97", "FNC2", "FNC2", "97", "411113"); 166 m_Code128.Rows.Add("98", "SHIFT", "SHIFT", "98", "411311"); 167 m_Code128.Rows.Add("99", "CODEC", "CODEC", "99", "113141"); 168 m_Code128.Rows.Add("100", "CODEB", "FNC4", "CODEB", "114131"); 169 m_Code128.Rows.Add("101", "FNC4", "CODEA", "CODEA", "311141"); 170 m_Code128.Rows.Add("102", "FNC1", "FNC1", "FNC1", "411131"); 171 m_Code128.Rows.Add("103", "StartA", "StartA", "StartA", "211412"); 172 m_Code128.Rows.Add("104", "StartB", "StartB", "StartB", "211214"); 173 m_Code128.Rows.Add("105", "StartC", "StartC", "StartC", "211232"); 174 m_Code128.Rows.Add("106", "Stop", "Stop", "Stop", "2331112"); 175 #endregion 176 } 177 /// <summary> 178 /// 获取128图形 179 /// </summary> 180 /// <param name="p_Text">文字</param> 181 /// <param name="p_Code">编码</param> 182 /// <returns>图形</returns> 183 public Bitmap GetCodeImage(string p_Text, Encode p_Code) 184 { 185 string _ViewText = p_Text; 186 string _Text = ""; 187 IList<int> _TextNumb = new List<int>(); 188 int _Examine = 0; //首位 189 switch (p_Code) 190 { 191 case Encode.Code128C: 192 _Examine = 105; 193 if (!((p_Text.Length & 1) == 0)) throw new Exception("128C长度必须是偶数"); 194 while (p_Text.Length != 0) 195 { 196 int _Temp = 0; 197 try 198 { 199 int _CodeNumb128 = Int32.Parse(p_Text.Substring(0, 2)); 200 } 201 catch 202 { 203 throw new Exception("128C必须是数字!"); 204 } 205 _Text += GetValue(p_Code, p_Text.Substring(0, 2), ref _Temp); 206 _TextNumb.Add(_Temp); 207 p_Text = p_Text.Remove(0, 2); 208 } 209 break; 210 case Encode.EAN128: 211 _Examine = 105; 212 if (!((p_Text.Length & 1) == 0)) throw new Exception("EAN128长度必须是偶数"); 213 _TextNumb.Add(102); 214 _Text += "411131"; 215 while (p_Text.Length != 0) 216 { 217 int _Temp = 0; 218 try 219 { 220 int _CodeNumb128 = Int32.Parse(p_Text.Substring(0, 2)); 221 } 222 catch 223 { 224 throw new Exception("128C必须是数字!"); 225 } 226 _Text += GetValue(Encode.Code128C, p_Text.Substring(0, 2), ref _Temp); 227 _TextNumb.Add(_Temp); 228 p_Text = p_Text.Remove(0, 2); 229 } 230 break; 231 default: 232 if (p_Code == Encode.Code128A) 233 { 234 _Examine = 103; 235 } 236 else 237 { 238 _Examine = 104; 239 } 240 while (p_Text.Length != 0) 241 { 242 int _Temp = 0; 243 string _ValueCode = GetValue(p_Code, p_Text.Substring(0, 1), ref _Temp); 244 if (_ValueCode.Length == 0) throw new Exception("无效的字符集!" + p_Text.Substring(0, 1).ToString()); 245 _Text += _ValueCode; 246 _TextNumb.Add(_Temp); 247 p_Text = p_Text.Remove(0, 1); 248 } 249 break; 250 } 251 if (_TextNumb.Count == 0) throw new Exception("错误的编码,无数据"); 252 _Text = _Text.Insert(0, GetValue(_Examine)); //获取开始位 253 for (int i = 0; i != _TextNumb.Count; i++) 254 { 255 _Examine += _TextNumb[i] * (i + 1); 256 } 257 _Examine = _Examine % 103; //获得严效位 258 _Text += GetValue(_Examine); //获取严效位 259 _Text += "2331112"; //结束位 260 Bitmap _CodeImage = GetImage(_Text); 261 GetViewText(_CodeImage, _ViewText); 262 return _CodeImage; 263 } 264 265 /// <summary> 266 /// 获取目标对应的数据 267 /// </summary> 268 /// <param name="p_Code">编码</param> 269 /// <param name="p_Value">数值 A b 30</param> 270 /// <param name="p_SetID">返回编号</param> 271 /// <returns>编码</returns> 272 private string GetValue(Encode p_Code, string p_Value, ref int p_SetID) 273 { 274 if (m_Code128 == null) return ""; 275 DataRow[] _Row = m_Code128.Select(p_Code.ToString() + "='" + p_Value + "'"); 276 if (_Row.Length != 1) throw new Exception("错误的编码" + p_Value.ToString()); 277 p_SetID = Int32.Parse(_Row[0]["ID"].ToString()); 278 return _Row[0]["BandCode"].ToString(); 279 } 280 /// <summary> 281 /// 根据编号获得条纹 282 /// </summary> 283 /// <param name="p_CodeId"></param> 284 /// <returns></returns> 285 private string GetValue(int p_CodeId) 286 { 287 DataRow[] _Row = m_Code128.Select("ID='" + p_CodeId.ToString() + "'"); 288 if (_Row.Length != 1) throw new Exception("验效位的编码错误" + p_CodeId.ToString()); 289 return _Row[0]["BandCode"].ToString(); 290 } 291 /// <summary> 292 /// 获得条码图形 293 /// </summary> 294 /// <param name="p_Text">文字</param> 295 /// <returns>图形</returns> 296 private Bitmap GetImage(string p_Text) 297 { 298 char[] _Value = p_Text.ToCharArray(); 299 int _Width = 0; 300 for (int i = 0; i != _Value.Length; i++) 301 { 302 _Width += Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1); 303 } 304 Bitmap _CodeImage = new Bitmap(_Width, (int)m_Height); 305 Graphics _Garphics = Graphics.FromImage(_CodeImage); 306 //Pen _Pen; 307 int _LenEx = 0; 308 for (int i = 0; i != _Value.Length; i++) 309 { 310 int _ValueNumb = Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1); //获取宽和放大系数 311 if (!((i & 1) == 0)) 312 { 313 //_Pen = new Pen(Brushes.White, _ValueNumb); 314 _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, 0, _ValueNumb, (int)m_Height)); 315 } 316 else 317 { 318 //_Pen = new Pen(Brushes.Black, _ValueNumb); 319 _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, 0, _ValueNumb, (int)m_Height)); 320 } 321 //_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height)); 322 _LenEx += _ValueNumb; 323 } 324 _Garphics.Dispose(); 325 return _CodeImage; 326 } 327 /// <summary> 328 /// 显示可见条码文字 如果小于40 不显示文字 329 /// </summary> 330 /// <param name="p_Bitmap">图形</param> 331 private void GetViewText(Bitmap p_Bitmap, string p_ViewText) 332 { 333 if (m_ValueFont == null) return; 334 Graphics _Graphics = Graphics.FromImage(p_Bitmap); 335 SizeF _DrawSize = _Graphics.MeasureString(p_ViewText, m_ValueFont); 336 if (_DrawSize.Height > p_Bitmap.Height - 10 || _DrawSize.Width > p_Bitmap.Width) 337 { 338 _Graphics.Dispose(); 339 return; 340 } 341 int _StarY = p_Bitmap.Height - (int)_DrawSize.Height; 342 _Graphics.FillRectangle(Brushes.White, new Rectangle(0, _StarY, p_Bitmap.Width, (int)_DrawSize.Height)); 343 _Graphics.DrawString(p_ViewText, m_ValueFont, Brushes.Black, 0, _StarY); 344 } 345 } 346 } 347 }
[代码转自]http://blog.csdn.net/anchenyanyue/article/details/7666284
现实中很多现有的代码库有code128码,但是没有ean128码,所以根据算法我们也可以很容易添加ean128.
一下是在barcodelib添加ean128的代码,这边只有code128部分,就当作是扩展吧 【BarcodeLibExtend下载】