C# GDI+实现BarCode条形码

本篇文章介绍了,BarCode条形码基于C# GDI+ 的实现方法详解。

条形码在生活中的应用非常广泛,具体的条形码知识大家自行百度,了解条形码知识对理解下面的代码是必要的。如果只是应用的话,直接拿去就可以用了。
废话不多说,上代码

复制代码代码如下:
  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上,就可以看到结果了。

以上代码需要引用 System.Drawing 命名空间。

顺带提一句,还有一种最简单的办法就是下载条形码字体,然后输出的时候设定字体为条形码字体,显示的就是条形码了。不过你不能要求每个客户机都装这种字体。这个是个明显的缺陷,所以不推荐使用这种方法。

posted @ 2024-03-01 15:14  soliang  阅读(53)  评论(0编辑  收藏  举报