`
hulianwang2014
  • 浏览: 692813 次
文章分类
社区版块
存档分类
最新评论
  • bcworld: 排版成这样,一点看的欲望都没有了
    jfinal

.NET绘制条码Code128A,Code128B,Code128C,EAN128

 
阅读更多

发一个CODE128条码绘制类。只支持Code128A,Code128B,Code128C,EAN128。

128C和EAN128是双位的也就是本身码一次取两位。而且必须是数字。EAN128和128C不同在与 EAN128前比128C多了一个EAN位(102),多的这一位是参与验效的。

如 2008 128C验效 (105 + (1 * 20+ 2 *8 )) % 103

EAN验效 (105+(1*102+2*20+3*8))%103 这里的102是EAN的头位。

属性
Height 绘制图形的高度

ValueFont是否显示可见号码 如果为NULL不显示号码。如果字体绘制如果大于条码所用的大小,即使设置了FONT也不会显示。
Magnify放大倍数

使用

  1. BandCode.Code128_Code= new BandCode.Code128();
  2. _Code.ValueFont= new Font( "宋体" ,10);
  3. pictureBox1.Image=_Code.GetCodeImage( "20081011001010020001" ,BandCode.Code128.Encode.Code128C);
  4. pictureBox1.Image.Save(@ "C:/B.BMP" );

完整的类

  1. using System;
  2. using System.Data;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Drawing;
  6. namespace CopyDirectoryInfo
  7. {
  8. ///<summary>
  9. ///Code128A,Code128B,Code128C,EAN128条码
  10. ///zgke@sina.com
  11. ///qq:116149
  12. ///</summary>
  13. public class BandCode
  14. {
  15. public class Code128
  16. {
  17. private DataTablem_Code128= new DataTable();
  18. private uint m_Height=40;
  19. ///<summary>
  20. ///高度
  21. ///</summary>
  22. public uint Height{ get { return m_Height;} set {m_Height=value;}}
  23. private Fontm_ValueFont= null ;
  24. ///<summary>
  25. ///是否显示可见号码如果为NULL不显示号码
  26. ///</summary>
  27. public FontValueFont{ get { return m_ValueFont;} set {m_ValueFont=value;}}
  28. private byte m_Magnify=0;
  29. ///<summary>
  30. ///放大倍数
  31. ///</summary>
  32. public byte Magnify{ get { return m_Magnify;} set {m_Magnify=value;}}
  33. ///<summary>
  34. ///条码类别
  35. ///</summary>
  36. public enum Encode
  37. {
  38. Code128A,
  39. Code128B,
  40. Code128C,
  41. EAN128
  42. }
  43. public Code128()
  44. {
  45. m_Code128.Columns.Add( "ID" );
  46. m_Code128.Columns.Add( "Code128A" );
  47. m_Code128.Columns.Add( "Code128B" );
  48. m_Code128.Columns.Add( "Code128C" );
  49. m_Code128.Columns.Add( "BandCode" );
  50. m_Code128.CaseSensitive= true ;
  51. #region数据表
  52. m_Code128.Rows.Add( "0" , "" , "" , "00" , "212222" );
  53. m_Code128.Rows.Add( "1" , "!" , "!" , "01" , "222122" );
  54. m_Code128.Rows.Add( "2" , "/"" , "/"" , "02" , "222221" );
  55. m_Code128.Rows.Add( "3" , "#" , "#" , "03" , "121223" );
  56. m_Code128.Rows.Add( "4" , "$" , "$" , "04" , "121322" );
  57. m_Code128.Rows.Add( "5" , "%" , "%" , "05" , "131222" );
  58. m_Code128.Rows.Add( "6" , "&" , "&" , "06" , "122213" );
  59. m_Code128.Rows.Add( "7" , "'" , "'" , "07" , "122312" );
  60. m_Code128.Rows.Add( "8" , "(" , "(" , "08" , "132212" );
  61. m_Code128.Rows.Add( "9" , ")" , ")" , "09" , "221213" );
  62. m_Code128.Rows.Add( "10" , "*" , "*" , "10" , "221312" );
  63. m_Code128.Rows.Add( "11" , "+" , "+" , "11" , "231212" );
  64. m_Code128.Rows.Add( "12" , "," , "," , "12" , "112232" );
  65. m_Code128.Rows.Add( "13" , "-" , "-" , "13" , "122132" );
  66. m_Code128.Rows.Add( "14" , "." , "." , "14" , "122231" );
  67. m_Code128.Rows.Add( "15" , "/" , "/" , "15" , "113222" );
  68. m_Code128.Rows.Add( "16" , "0" , "0" , "16" , "123122" );
  69. m_Code128.Rows.Add( "17" , "1" , "1" , "17" , "123221" );
  70. m_Code128.Rows.Add( "18" , "2" , "2" , "18" , "223211" );
  71. m_Code128.Rows.Add( "19" , "3" , "3" , "19" , "221132" );
  72. m_Code128.Rows.Add( "20" , "4" , "4" , "20" , "221231" );
  73. m_Code128.Rows.Add( "21" , "5" , "5" , "21" , "213212" );
  74. m_Code128.Rows.Add( "22" , "6" , "6" , "22" , "223112" );
  75. m_Code128.Rows.Add( "23" , "7" , "7" , "23" , "312131" );
  76. m_Code128.Rows.Add( "24" , "8" , "8" , "24" , "311222" );
  77. m_Code128.Rows.Add( "25" , "9" , "9" , "25" , "321122" );
  78. m_Code128.Rows.Add( "26" , ":" , ":" , "26" , "321221" );
  79. m_Code128.Rows.Add( "27" , ";" , ";" , "27" , "312212" );
  80. m_Code128.Rows.Add( "28" , "<" , "<" , "28" , "322112" );
  81. m_Code128.Rows.Add( "29" , "=" , "=" , "29" , "322211" );
  82. m_Code128.Rows.Add( "30" , ">" , ">" , "30" , "212123" );
  83. m_Code128.Rows.Add( "31" , "?" , "?" , "31" , "212321" );
  84. m_Code128.Rows.Add( "32" , "@" , "@" , "32" , "232121" );
  85. m_Code128.Rows.Add( "33" , "A" , "A" , "33" , "111323" );
  86. m_Code128.Rows.Add( "34" , "B" , "B" , "34" , "131123" );
  87. m_Code128.Rows.Add( "35" , "C" , "C" , "35" , "131321" );
  88. m_Code128.Rows.Add( "36" , "D" , "D" , "36" , "112313" );
  89. m_Code128.Rows.Add( "37" , "E" , "E" , "37" , "132113" );
  90. m_Code128.Rows.Add( "38" , "F" , "F" , "38" , "132311" );
  91. m_Code128.Rows.Add( "39" , "G" , "G" , "39" , "211313" );
  92. m_Code128.Rows.Add( "40" , "H" , "H" , "40" , "231113" );
  93. m_Code128.Rows.Add( "41" , "I" , "I" , "41" , "231311" );
  94. m_Code128.Rows.Add( "42" , "J" , "J" , "42" , "112133" );
  95. m_Code128.Rows.Add( "43" , "K" , "K" , "43" , "112331" );
  96. m_Code128.Rows.Add( "44" , "L" , "L" , "44" , "132131" );
  97. m_Code128.Rows.Add( "45" , "M" , "M" , "45" , "113123" );
  98. m_Code128.Rows.Add( "46" , "N" , "N" , "46" , "113321" );
  99. m_Code128.Rows.Add( "47" , "O" , "O" , "47" , "133121" );
  100. m_Code128.Rows.Add( "48" , "P" , "P" , "48" , "313121" );
  101. m_Code128.Rows.Add( "49" , "Q" , "Q" , "49" , "211331" );
  102. m_Code128.Rows.Add( "50" , "R" , "R" , "50" , "231131" );
  103. m_Code128.Rows.Add( "51" , "S" , "S" , "51" , "213113" );
  104. m_Code128.Rows.Add( "52" , "T" , "T" , "52" , "213311" );
  105. m_Code128.Rows.Add( "53" , "U" , "U" , "53" , "213131" );
  106. m_Code128.Rows.Add( "54" , "V" , "V" , "54" , "311123" );
  107. m_Code128.Rows.Add( "55" , "W" , "W" , "55" , "311321" );
  108. m_Code128.Rows.Add( "56" , "X" , "X" , "56" , "331121" );
  109. m_Code128.Rows.Add( "57" , "Y" , "Y" , "57" , "312113" );
  110. m_Code128.Rows.Add( "58" , "Z" , "Z" , "58" , "312311" );
  111. m_Code128.Rows.Add( "59" , "[" , "[" , "59" , "332111" );
  112. m_Code128.Rows.Add( "60" , "//"," // "," 60 "," 314111");
  113. m_Code128.Rows.Add( "61" , "]" , "]" , "61" , "221411" );
  114. m_Code128.Rows.Add( "62" , "^" , "^" , "62" , "431111" );
  115. m_Code128.Rows.Add( "63" , "_" , "_" , "63" , "111224" );
  116. m_Code128.Rows.Add( "64" , "NUL" , "`" , "64" , "111422" );
  117. m_Code128.Rows.Add( "65" , "SOH" , "a" , "65" , "121124" );
  118. m_Code128.Rows.Add( "66" , "STX" , "b" , "66" , "121421" );
  119. m_Code128.Rows.Add( "67" , "ETX" , "c" , "67" , "141122" );
  120. m_Code128.Rows.Add( "68" , "EOT" , "d" , "68" , "141221" );
  121. m_Code128.Rows.Add( "69" , "ENQ" , "e" , "69" , "112214" );
  122. m_Code128.Rows.Add( "70" , "ACK" , "f" , "70" , "112412" );
  123. m_Code128.Rows.Add( "71" , "BEL" , "g" , "71" , "122114" );
  124. m_Code128.Rows.Add( "72" , "BS" , "h" , "72" , "122411" );
  125. m_Code128.Rows.Add( "73" , "HT" , "i" , "73" , "142112" );
  126. m_Code128.Rows.Add( "74" , "LF" , "j" , "74" , "142211" );
  127. m_Code128.Rows.Add( "75" , "VT" , "k" , "75" , "241211" );
  128. m_Code128.Rows.Add( "76" , "FF" , "I" , "76" , "221114" );
  129. m_Code128.Rows.Add( "77" , "CR" , "m" , "77" , "413111" );
  130. m_Code128.Rows.Add( "78" , "SO" , "n" , "78" , "241112" );
  131. m_Code128.Rows.Add( "79" , "SI" , "o" , "79" , "134111" );
  132. m_Code128.Rows.Add( "80" , "DLE" , "p" , "80" , "111242" );
  133. m_Code128.Rows.Add( "81" , "DC1" , "q" , "81" , "121142" );
  134. m_Code128.Rows.Add( "82" , "DC2" , "r" , "82" , "121241" );
  135. m_Code128.Rows.Add( "83" , "DC3" , "s" , "83" , "114212" );
  136. m_Code128.Rows.Add( "84" , "DC4" , "t" , "84" , "124112" );
  137. m_Code128.Rows.Add( "85" , "NAK" , "u" , "85" , "124211" );
  138. m_Code128.Rows.Add( "86" , "SYN" , "v" , "86" , "411212" );
  139. m_Code128.Rows.Add( "87" , "ETB" , "w" , "87" , "421112" );
  140. m_Code128.Rows.Add( "88" , "CAN" , "x" , "88" , "421211" );
  141. m_Code128.Rows.Add( "89" , "EM" , "y" , "89" , "212141" );
  142. m_Code128.Rows.Add( "90" , "SUB" , "z" , "90" , "214121" );
  143. m_Code128.Rows.Add( "91" , "ESC" , "{" , "91" , "412121" );
  144. m_Code128.Rows.Add( "92" , "FS" , "|" , "92" , "111143" );
  145. m_Code128.Rows.Add( "93" , "GS" , "}" , "93" , "111341" );
  146. m_Code128.Rows.Add( "94" , "RS" , "~" , "94" , "131141" );
  147. m_Code128.Rows.Add( "95" , "US" , "DEL" , "95" , "114113" );
  148. m_Code128.Rows.Add( "96" , "FNC3" , "FNC3" , "96" , "114311" );
  149. m_Code128.Rows.Add( "97" , "FNC2" , "FNC2" , "97" , "411113" );
  150. m_Code128.Rows.Add( "98" , "SHIFT" , "SHIFT" , "98" , "411311" );
  151. m_Code128.Rows.Add( "99" , "CODEC" , "CODEC" , "99" , "113141" );
  152. m_Code128.Rows.Add( "100" , "CODEB" , "FNC4" , "CODEB" , "114131" );
  153. m_Code128.Rows.Add( "101" , "FNC4" , "CODEA" , "CODEA" , "311141" );
  154. m_Code128.Rows.Add( "102" , "FNC1" , "FNC1" , "FNC1" , "411131" );
  155. m_Code128.Rows.Add( "103" , "StartA" , "StartA" , "StartA" , "211412" );
  156. m_Code128.Rows.Add( "104" , "StartB" , "StartB" , "StartB" , "211214" );
  157. m_Code128.Rows.Add( "105" , "StartC" , "StartC" , "StartC" , "211232" );
  158. m_Code128.Rows.Add( "106" , "Stop" , "Stop" , "Stop" , "2331112" );
  159. #endregion
  160. }
  161. ///<summary>
  162. ///获取128图形
  163. ///</summary>
  164. ///<paramname="p_Text">文字</param>
  165. ///<paramname="p_Code">编码</param>
  166. ///<returns>图形</returns>
  167. public BitmapGetCodeImage( string p_Text,Encodep_Code)
  168. {
  169. string _ViewText=p_Text;
  170. string _Text= "" ;
  171. IList< int >_TextNumb= new List< int >();
  172. int _Examine=0; //首位
  173. switch (p_Code)
  174. {
  175. case Encode.Code128C:
  176. _Examine=105;
  177. if (!((p_Text.Length&1)==0)) throw new Exception( "128C长度必须是偶数" );
  178. while (p_Text.Length!=0)
  179. { int _Temp=0;
  180. try
  181. {
  182. int _CodeNumb128=Int32.Parse(p_Text.Substring(0,2));
  183. }
  184. catch
  185. {
  186. throw new Exception( "128C必须是数字!" );
  187. }
  188. _Text+=GetValue(p_Code,p_Text.Substring(0,2), ref _Temp);
  189. _TextNumb.Add(_Temp);
  190. p_Text=p_Text.Remove(0,2);
  191. }
  192. break ;
  193. case Encode.EAN128:
  194. _Examine=105;
  195. if (!((p_Text.Length&1)==0)) throw new Exception( "EAN128长度必须是偶数" );
  196. _TextNumb.Add(102);
  197. _Text+= "411131" ;
  198. while (p_Text.Length!=0)
  199. {
  200. int _Temp=0;
  201. try
  202. {
  203. int _CodeNumb128=Int32.Parse(p_Text.Substring(0,2));
  204. }
  205. catch
  206. {
  207. throw new Exception( "128C必须是数字!" );
  208. }
  209. _Text+=GetValue(Encode.Code128C,p_Text.Substring(0,2), ref _Temp);
  210. _TextNumb.Add(_Temp);
  211. p_Text=p_Text.Remove(0,2);
  212. }
  213. break ;
  214. default :
  215. if (p_Code==Encode.Code128A)
  216. {
  217. _Examine=103;
  218. }
  219. else
  220. {
  221. _Examine=104;
  222. }
  223. while (p_Text.Length!=0)
  224. {
  225. int _Temp=0;
  226. string _ValueCode=GetValue(p_Code,p_Text.Substring(0,1), ref _Temp);
  227. if (_ValueCode.Length==0) throw new Exception( "无效的字符集!" +p_Text.Substring(0,1).ToString());
  228. _Text+=_ValueCode;
  229. _TextNumb.Add(_Temp);
  230. p_Text=p_Text.Remove(0,1);
  231. }
  232. break ;
  233. }
  234. if (_TextNumb.Count==0) throw new Exception( "错误的编码,无数据" );
  235. _Text=_Text.Insert(0,GetValue(_Examine)); //获取开始位
  236. for ( int i=0;i!=_TextNumb.Count;i++)
  237. {
  238. _Examine+=_TextNumb[i]*(i+1);
  239. }
  240. _Examine=_Examine%103; //获得严效位
  241. _Text+=GetValue(_Examine); //获取严效位
  242. _Text+= "2331112" ; //结束位
  243. Bitmap_CodeImage=GetImage(_Text);
  244. GetViewText(_CodeImage,_ViewText);
  245. return _CodeImage;
  246. }
  247. ///<summary>
  248. ///获取目标对应的数据
  249. ///</summary>
  250. ///<paramname="p_Code">编码</param>
  251. ///<paramname="p_Value">数值Ab30</param>
  252. ///<paramname="p_SetID">返回编号</param>
  253. ///<returns>编码</returns>
  254. private string GetValue(Encodep_Code, string p_Value, ref int p_SetID)
  255. {
  256. if (m_Code128== null ) return "" ;
  257. DataRow[]_Row=m_Code128.Select(p_Code.ToString()+ "='" +p_Value+ "'" );
  258. if (_Row.Length!=1) throw new Exception( "错误的编码" +p_Value.ToString());
  259. p_SetID=Int32.Parse(_Row[0][ "ID" ].ToString());
  260. return _Row[0][ "BandCode" ].ToString();
  261. }
  262. ///<summary>
  263. ///根据编号获得条纹
  264. ///</summary>
  265. ///<paramname="p_CodeId"></param>
  266. ///<returns></returns>
  267. private string GetValue( int p_CodeId)
  268. {
  269. DataRow[]_Row=m_Code128.Select( "ID='" +p_CodeId.ToString()+ "'" );
  270. if (_Row.Length!=1) throw new Exception( "验效位的编码错误" +p_CodeId.ToString());
  271. return _Row[0][ "BandCode" ].ToString();
  272. }
  273. ///<summary>
  274. ///获得条码图形
  275. ///</summary>
  276. ///<paramname="p_Text">文字</param>
  277. ///<returns>图形</returns>
  278. private BitmapGetImage( string p_Text)
  279. {
  280. char []_Value=p_Text.ToCharArray();
  281. int _Width=0;
  282. for ( int i=0;i!=_Value.Length;i++)
  283. {
  284. _Width+=Int32.Parse(_Value[i].ToString())*(m_Magnify+1);
  285. }
  286. Bitmap_CodeImage= new Bitmap(_Width,( int )m_Height);
  287. Graphics_Garphics=Graphics.FromImage(_CodeImage);
  288. //Pen_Pen;
  289. int _LenEx=0;
  290. for ( int i=0;i!=_Value.Length;i++)
  291. {
  292. int _ValueNumb=Int32.Parse(_Value[i].ToString())*(m_Magnify+1); //获取宽和放大系数
  293. if (!((i&1)==0))
  294. {
  295. //_Pen=newPen(Brushes.White,_ValueNumb);
  296. _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx,0,_ValueNumb,( int )m_Height));
  297. }
  298. else
  299. {
  300. //_Pen=newPen(Brushes.Black,_ValueNumb);
  301. _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx,0,_ValueNumb,( int )m_Height));
  302. }
  303. //_Garphics.(_Pen,newPoint(_LenEx,0),newPoint(_LenEx,m_Height));
  304. _LenEx+=_ValueNumb;
  305. }
  306. _Garphics.Dispose();
  307. return _CodeImage;
  308. }
  309. ///<summary>
  310. ///显示可见条码文字如果小于40不显示文字
  311. ///</summary>
  312. ///<paramname="p_Bitmap">图形</param>
  313. private void GetViewText(Bitmapp_Bitmap, string p_ViewText)
  314. {
  315. if (m_ValueFont== null ) return ;
  316. Graphics_Graphics=Graphics.FromImage(p_Bitmap);
  317. SizeF_DrawSize=_Graphics.MeasureString(p_ViewText,m_ValueFont);
  318. if (_DrawSize.Height>p_Bitmap.Height-10||_DrawSize.Width>p_Bitmap.Width)
  319. {
  320. _Graphics.Dispose();
  321. return ;
  322. }
  323. int _StarY=p_Bitmap.Height-( int )_DrawSize.Height;
  324. _Graphics.FillRectangle(Brushes.White, new Rectangle(0,_StarY,p_Bitmap.Width,( int )_DrawSize.Height));
  325. _Graphics.DrawString(p_ViewText,m_ValueFont,Brushes.Black,0,_StarY);
  326. }
  327. //12345678
  328. //(105+(1*12+2*34+3*56+4*78))%103=47
  329. //结果为starc+12+34+56+78+47+end
  330. }
  331. }
  332. }
http://blog.csdn.net/zgke/archive/2008/12/10/3488866.aspx
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics