C#如何打印条形码EAN13码

转载 2015年07月08日 16:16:03


条形码相信大家都不陌生,但自己写程序打印条形码就不是很容易了。最近用条码字体打印了39码,觉的太长,不美观。后来,在国外网站(http://www.c-sharpcorner.com//Code/2004/March/EANBarcode.asp)上找到了EAN13码的源码,修改了其中的一些小Bug,发上来与大家共享。
附EAN13码的计算规则:
        假设将EAN的内容从右到左的顺序编号:N13、N12、N11、N10、N9、N8、N7、N6、N5、N4、N3、N2、N1,其中N1是准备要求的校验位:
1、C1=(N2+N4+N6+N8+N10+N12)*3;
2、C2=N3+N5+N7+N9+N11+N13;
3、将C1与C2相加,取其个位数,设为C3;4、校验位N1=10-C3

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace BarCodeEAN13

 /// <summary>
 /// Barcode control.
 /// </summary>
 public class wctrl_BarCode
 {

  private static string getENA13Code(string Numbers12bit)
  {
   int c1 = 0;
   int c2 = 0;

   for (int i=0; i<11;  i= i+2)
   {
    c1 += int.Parse(Numbers12bit[i].ToString());
    c2 += int.Parse(Numbers12bit[i+1].ToString());
   }

   int c3 = c1 + c2 *3;

   c3 = c3  - c3/10 *10;

   if (c3 == 0)
   {
    return Numbers12bit + 0;
   }
   else
   {
    int N = 10 - c3;

    return Numbers12bit + N;
   }
  }

  /// <summary>
  /// Paint EAN13 barcode to specified graphics into specified draw rectangle.
  /// </summary>
  /// <param name="barCode">BarCode value.</param>
  /// <param name="g">Graphics where to draw.</param>
  /// <param name="drawBounds">Draw bounds.</param>
  public static void Paint_EAN13(string Numbers12bit,Graphics g,Rectangle drawBounds)
  {
   string barCode = getENA13Code(Numbers12bit);

   char[] symbols = barCode.ToCharArray();

   //--- Validate barCode -------------------------------------------------------------------//
   if(barCode.Length != 13){    
    return;
   }
   foreach(char c in symbols){
    if(!Char.IsDigit(c)){
     return;
    }
   }

   //--- Check barcode checksum ------------------------//
   int checkSum = Convert.ToInt32(symbols[12].ToString());
   int calcSum  = 0;
   bool one_three = true;
   for(int i=0;i<12;i++){
    if(one_three){
     calcSum += (Convert.ToInt32(symbols[i].ToString()) * 1);
     one_three = false;
    }
    else{
     calcSum += (Convert.ToInt32(symbols[i].ToString()) * 3);
     one_three = true;
    }    
   }
   
   char[] calcSumChar = calcSum.ToString().ToCharArray();
   if(checkSum !=0 && checkSum != (10 - Convert.ToInt32(calcSumChar[calcSumChar.Length - 1].ToString()))){
    return;
      }   
   //--------------------------------------------------//
   //---------------------------------------------------------------------------------------//

   Font font = new Font("Microsoft Sans Serif",8);
   
   // Fill backround with white color
//   g.Clear(Color.White);

   int lineWidth = 1;
   int x = drawBounds.X;

   // Paint human readable 1 system symbol code
   g.DrawString(symbols[0].ToString(),font,new SolidBrush(Color.Black),x,drawBounds.Y + drawBounds.Height - 16);
   x += 10;

   // Paint left 'guard bars', always same '101'
   g.DrawLine(new Pen(Color.Black,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height);
   x += lineWidth;
   g.DrawLine(new Pen(Color.White,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height);
   x += lineWidth;
   g.DrawLine(new Pen(Color.Black,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height);
   x += lineWidth;

   // First number of barcode specifies how to encode each character in the left-hand
   // side of the barcode should be encoded.
   bool[] leftSideParity = new bool[6];
   switch(symbols[0])
   {
    case '0':
     leftSideParity[0] = true;  // Odd
     leftSideParity[1] = true;  // Odd
     leftSideParity[2] = true;  // Odd
     leftSideParity[3] = true;  // Odd
     leftSideParity[4] = true;  // Odd
     leftSideParity[5] = true;  // Odd
     break;
    case '1':
     leftSideParity[0] = true;  // Odd
     leftSideParity[1] = true;  // Odd
     leftSideParity[2] = false; // Even
     leftSideParity[3] = true;  // Odd
     leftSideParity[4] = false; // Even
     leftSideParity[5] = false; // Even
     break;
    case '2':
     leftSideParity[0] = true;  // Odd
     leftSideParity[1] = true;  // Odd
     leftSideParity[2] = false; // Even
     leftSideParity[3] = false; // Even
     leftSideParity[4] = true;  // Odd
     leftSideParity[5] = false; // Even
     break;
    case '3':
     leftSideParity[0] = true;  // Odd
     leftSideParity[1] = true;  // Odd
     leftSideParity[2] = false; // Even
     leftSideParity[3] = false; // Even
     leftSideParity[4] = false; // Even
     leftSideParity[5] = true;  // Odd
     break;
    case '4':
     leftSideParity[0] = true;  // Odd
     leftSideParity[1] = false; // Even
     leftSideParity[2] = true;  // Odd
     leftSideParity[3] = true;  // Odd
     leftSideParity[4] = false; // Even
     leftSideParity[5] = false; // Even
     break;
    case '5':
     leftSideParity[0] = true;  // Odd
     leftSideParity[1] = false; // Even
     leftSideParity[2] = false; // Even
     leftSideParity[3] = true;  // Odd
     leftSideParity[4] = true;  // Odd
     leftSideParity[5] = false; // Even
     break;
    case '6':
     leftSideParity[0] = true;  // Odd
     leftSideParity[1] = false; // Even
     leftSideParity[2] = false; // Even
     leftSideParity[3] = false; // Even
     leftSideParity[4] = true;  // Odd
     leftSideParity[5] = true;  // Odd
     break;
    case '7':
     leftSideParity[0] = true;  // Odd
     leftSideParity[1] = false; // Even
     leftSideParity[2] = true;  // Odd
     leftSideParity[3] = false; // Even
     leftSideParity[4] = true;  // Odd
     leftSideParity[5] = false; // Even
     break;
    case '8':
     leftSideParity[0] = true;  // Odd
     leftSideParity[1] = false; // Even
     leftSideParity[2] = true;  // Odd
     leftSideParity[3] = false; // Even
     leftSideParity[4] = false; // Even
     leftSideParity[5] = true;  // Odd
     break;
    case '9':
     leftSideParity[0] = true;  // Odd
     leftSideParity[1] = false; // Even
     leftSideParity[2] = false; // Even
     leftSideParity[3] = true;  // Odd
     leftSideParity[4] = false; // Even
     leftSideParity[5] = true;  // Odd
     break;
   }

   // second number system digit + 5 symbol manufacter code
   string lines = "";
   for(int i=0;i<6;i++){
    bool oddParity = leftSideParity[i];
    if(oddParity){
     switch(symbols[i + 1])
     {
      case '0':
       lines += "0001101";
       break;
      case '1':
       lines += "0011001";
       break;
      case '2':
       lines += "0010011";
       break;
      case '3':
       lines += "0111101";
       break;
      case '4':
       lines += "0100011";
       break;
      case '5':
       lines += "0110001";
       break;
      case '6':
       lines += "0101111";
       break;
      case '7':
       lines += "0111011";
       break;
      case '8':
       lines += "0110111";
       break;
      case '9':
       lines += "0001011";
       break;
     }
    }
    // Even parity
    else{
     switch(symbols[i + 1])
     {
      case '0':
       lines += "0100111";
       break;
      case '1':
       lines += "0110011";
       break;
      case '2':
       lines += "0011011";
       break;
      case '3':
       lines += "0100001";
       break;
      case '4':
       lines += "0011101";
       break;
      case '5':
       lines += "0111001";
       break;
      case '6':
       lines += "0000101";
       break;
      case '7':
       lines += "0010001";
       break;
      case '8':
       lines += "0001001";
       break;
      case '9':
       lines += "0010111";
       break;
     }
    }
   }

   // Paint human readable left-side 6 symbol code
   g.DrawString(barCode.Substring(1,6),font,new SolidBrush(Color.Black),x,drawBounds.Y + drawBounds.Height - 12);

   char[] xxx = lines.ToCharArray();
   for(int i=0;i<xxx.Length;i++){
    if(xxx[i] == '1'){
     g.DrawLine(new Pen(Color.Black,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height - 12);
    }
    else{
     g.DrawLine(new Pen(Color.White,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height - 12);
    }
    x += lineWidth;
   }
   
   // Paint center 'guard bars', always same '01010'
   g.DrawLine(new Pen(Color.White,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height);
   x += lineWidth;
   g.DrawLine(new Pen(Color.Black,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height);
   x += lineWidth;
   g.DrawLine(new Pen(Color.White,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height);
   x += lineWidth;
   g.DrawLine(new Pen(Color.Black,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height);
   x += lineWidth;
   g.DrawLine(new Pen(Color.White,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height);
   x += lineWidth;

   // 5 symbol product code + 1 symbol parity
   lines = "";
   for(int i=7;i<13;i++){
    switch(symbols[i])
    {
     case '0':
      lines += "1110010";
      break;
     case '1':
      lines += "1100110";
      break;
     case '2':
      lines += "1101100";
      break;
     case '3':
      lines += "1000010";
      break;
     case '4':
      lines += "1011100";
      break;
     case '5':
      lines += "1001110";
      break;
     case '6':
      lines += "1010000";
      break;
     case '7':
      lines += "1000100";
      break;
     case '8':
      lines += "1001000";
      break;
     case '9':
      lines += "1110100";
      break;
    }
   }

   // Paint human readable left-side 6 symbol code
   g.DrawString(barCode.Substring(7,6),font,new SolidBrush(Color.Black),x,drawBounds.Y + drawBounds.Height - 12);

   xxx = lines.ToCharArray();
   for(int i=0;i<xxx.Length;i++){
    if(xxx[i] == '1'){
     g.DrawLine(new Pen(Color.Black,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height - 12);
    }
    else{
     g.DrawLine(new Pen(Color.White,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height - 12);
    }
    x += lineWidth;
   }
   
   // Paint right 'guard bars', always same '101'
   g.DrawLine(new Pen(Color.Black,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height);
   x += lineWidth;
   g.DrawLine(new Pen(Color.White,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height);
   x += lineWidth;
   g.DrawLine(new Pen(Color.Black,lineWidth),x,drawBounds.Y,x,drawBounds.Y + drawBounds.Height);
  }

 }
}

qt+zint实现打印一维条形码

通过zint实现打印条形码
  • zhang_ruiqiang
  • zhang_ruiqiang
  • 2015年10月09日 09:42
  • 2331

一维码EAN 13简介及其解码实现(zxing-cpp)

一维码EAN 13简介及其解码实现(zxing-cpp)
  • fengbingchun
  • fengbingchun
  • 2017年01月19日 21:39
  • 1211

HTML5+规范:barcode(条码扫描)

Barcode模块管理条码扫描,提供常见的条码(二维码及一维码)的扫描识别功能,可调用设备的摄像头对条码图片扫描进行数据输入。通过plus.barcode可获取条码码管理对象。 1、常量   (1...
  • qq_27626333
  • qq_27626333
  • 2016年07月03日 15:04
  • 19473

C#利用zxing.net生成二维码和条形码并实现打印的功能

C#利用zxing.net生成二维码和条形码以及实现打印的功能     开篇:zxing.net是.net平台下编解条形码和二维码的工具。          下载地址:http://pan.baidu...
  • jun502525164
  • jun502525164
  • 2014年04月03日 21:55
  • 37413

Delphi实现条码生成与打印实现代码

条形码或称条码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符。条形码技术主要原理是利用了光的反射,白色物体能反射各种波长的可见光,黑色物体则吸收各...
  • my98800
  • my98800
  • 2016年03月08日 09:40
  • 2633

C#打印小票自带条形码打印

private void btnReceipts_Click(object sender, EventArgs e) { string sheet_no = t...
  • chinaboykai
  • chinaboykai
  • 2016年04月09日 12:51
  • 2542

ZXing实现条形码、二维码

一、 基本介绍          1-1. ZXing是一个开源Java类库用于解析多种格式的条形码和二维码。官网:http://code.google.com/p/zxing/         ...
  • zmx729618
  • zmx729618
  • 2016年10月18日 10:26
  • 3395

基于C#生成条形码操作知识汇总附源码

 本文实例讲述了C#实现用于生成条形码的类。分享给大家供大家参考。具体如下: 这个C#类可以用来生成39码 12位标准条形码 ? ...
  • Andrewniu
  • Andrewniu
  • 2016年10月25日 15:36
  • 1138

【Qt】条形码制作器(Code39编码)

环境:Qt5.3 MSVC2010 编译器:Qt Creator 描述:         最近公司项目卡在了POS机打印小票上,因为涉及打印机太多,很多打印机对ESC/POS指令集支持又不是特别好...
  • shihoongbo
  • shihoongbo
  • 2015年10月22日 10:47
  • 2198

C#生成Code39条形码【非条形码字体】

Code39是条形码的一种。由于编制简单、能够对任意长度的数据进行编码、支持设备广泛等特性而被广泛采用。 能够对任意长度的数据进行编码。其局限在于印刷品的长度和条码阅读器的识别范围。 ...
  • juyaai0124
  • juyaai0124
  • 2015年07月30日 15:46
  • 703
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C#如何打印条形码EAN13码
举报原因:
原因补充:

(最多只允许输入30个字)