.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.          
  4.             pictureBox1.Image=_Code.GetCodeImage( "20081011001010020001" , BandCode.Code128.Encode.Code128C);
  5.             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  DataTable m_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  Font m_ValueFont =  null ;
  24.              /// <summary>
  25.              /// 是否显示可见号码  如果为NULL不显示号码
  26.              /// </summary>
  27.              public  Font ValueFont {  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.              /// <param name="p_Text">文字</param>
  165.              /// <param name="p_Code">编码</param>      
  166.              /// <returns>图形</returns>
  167.              public  Bitmap GetCodeImage( string  p_Text,Encode p_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.                         
  224.                          while  (p_Text.Length!=0)
  225.                         {
  226.                              int  _Temp=0;
  227.                              string  _ValueCode = GetValue(p_Code, p_Text.Substring(0, 1),  ref  _Temp);
  228.                              if  (_ValueCode.Length == 0)  throw   new  Exception( "无效的字符集!"  + p_Text.Substring(0, 1).ToString());
  229.                             _Text += _ValueCode;
  230.                             _TextNumb.Add(_Temp);
  231.                             p_Text = p_Text.Remove(0, 1);
  232.                         }                       
  233.                          break ;
  234.                 }
  235.                  if  (_TextNumb.Count == 0)  throw   new  Exception( "错误的编码,无数据" );
  236.                _Text= _Text.Insert(0, GetValue(_Examine));  //获取开始位
  237.                                 
  238.                  for  ( int  i = 0; i != _TextNumb.Count; i++)
  239.                 {
  240.                     _Examine += _TextNumb[i] * (i + 1);
  241.                 }
  242.                 _Examine = _Examine % 103;            //获得严效位
  243.                 _Text+= GetValue(_Examine);   //获取严效位
  244.                 _Text +=  "2331112"//结束位
  245.                 Bitmap _CodeImage = GetImage(_Text);
  246.                 GetViewText(_CodeImage, _ViewText);
  247.                  return  _CodeImage; 
  248.             }
  249.              /// <summary>
  250.              /// 获取目标对应的数据
  251.              /// </summary>
  252.              /// <param name="p_Code">编码</param>
  253.              /// <param name="p_Value">数值 A b  30</param>
  254.              /// <param name="p_SetID">返回编号</param>
  255.              /// <returns>编码</returns>
  256.              private   string  GetValue(Encode p_Code,  string  p_Value, ref   int  p_SetID)
  257.             {
  258.                  if  (m_Code128 ==  nullreturn   "" ;
  259.                 DataRow[] _Row = m_Code128.Select(p_Code.ToString()+ "='"  + p_Value +  "'" );
  260.                  if  (_Row.Length != 1)  throw   new  Exception( "错误的编码"  + p_Value.ToString());
  261.                 p_SetID = Int32.Parse(_Row[0][ "ID" ].ToString());
  262.                  return  _Row[0][ "BandCode" ].ToString();               
  263.             }
  264.              /// <summary>
  265.              /// 根据编号获得条纹
  266.              /// </summary>
  267.              /// <param name="p_CodeId"></param>
  268.              /// <returns></returns>
  269.              private   string  GetValue( int  p_CodeId)
  270.             {
  271.                 DataRow[] _Row = m_Code128.Select( "ID='"  + p_CodeId.ToString() +  "'" );
  272.                  if  (_Row.Length != 1)  throw   new  Exception( "验效位的编码错误"  + p_CodeId.ToString());               
  273.                  return  _Row[0][ "BandCode" ].ToString();        
  274.             }
  275.              /// <summary>
  276.              /// 获得条码图形
  277.              /// </summary>
  278.              /// <param name="p_Text">文字</param>
  279.              /// <returns>图形</returns>
  280.              private  Bitmap GetImage( string  p_Text)
  281.             {
  282.                  char [] _Value = p_Text.ToCharArray();
  283.                  int  _Width=0;
  284.                  for  ( int  i = 0; i != _Value.Length; i++)
  285.                 {
  286.                     _Width += Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1);
  287.                 }
  288.                 Bitmap _CodeImage =  new  Bitmap(_Width,( int ) m_Height);
  289.                 Graphics _Garphics= Graphics.FromImage(_CodeImage);           
  290.                  //Pen _Pen;
  291.                  int  _LenEx = 0;
  292.                  for  ( int  i = 0; i != _Value.Length; i++)
  293.                 {
  294.                      int  _ValueNumb = Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1);   //获取宽和放大系数
  295.                      if  (!((i & 1) == 0))
  296.                     {
  297.                          //_Pen = new Pen(Brushes.White, _ValueNumb);
  298.                         _Garphics.FillRectangle(Brushes.White,  new  Rectangle(_LenEx, 0, _ValueNumb,( int ) m_Height));
  299.                     }
  300.                      else
  301.                     {
  302.                          //_Pen = new Pen(Brushes.Black, _ValueNumb);
  303.                         _Garphics.FillRectangle(Brushes.Black,  new  Rectangle(_LenEx, 0, _ValueNumb,( int ) m_Height));
  304.                     }
  305.                      //_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
  306.                     _LenEx += _ValueNumb;
  307.                 }
  308.                 _Garphics.Dispose();
  309.                  return  _CodeImage;
  310.             }
  311.              /// <summary>
  312.              /// 显示可见条码文字 如果小于40 不显示文字
  313.              /// </summary>
  314.              /// <param name="p_Bitmap">图形</param>           
  315.              private   void  GetViewText(Bitmap p_Bitmap, string  p_ViewText)
  316.             {
  317.                  if  (m_ValueFont ==  nullreturn ;
  318.                
  319.                 Graphics _Graphics = Graphics.FromImage(p_Bitmap);                
  320.                 SizeF _DrawSize =_Graphics.MeasureString(p_ViewText,m_ValueFont);
  321.                  if  (_DrawSize.Height > p_Bitmap.Height-10 || _DrawSize.Width > p_Bitmap.Width)
  322.                 {
  323.                     _Graphics.Dispose();
  324.                      return ;
  325.                 }          
  326.     
  327.                  int  _StarY= p_Bitmap.Height-( int )_DrawSize.Height;
  328.                 _Graphics.FillRectangle(Brushes.White,  new  Rectangle(0, _StarY, p_Bitmap.Width, ( int )_DrawSize.Height));
  329.                 _Graphics.DrawString(p_ViewText, m_ValueFont, Brushes.Black, 0, _StarY);
  330.             }
  331.            
  332.              //12345678
  333.              //(105 + (1 * 12 + 2 * 34 + 3 * 56 + 4 *78)) % 103 = 47
  334.              //结果为starc +12 +34 +56 +78 +47 +end
  335.         }
  336.     }
  337. }
http://blog.csdn.net/zgke/archive/2008/12/10/3488866.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值