用Code128字体打印条码

项目已经到了尾声了,开发业快完了,拿到正是的条码纸和碳带,开打,加了'-'的128码,读取得速度不是很快,也不是很好读,因为是使用生成条码图片的方式打印,看来还是有问题,之前用过指令直接打印,条码倒是读的快,但是其他的文字的大小不可控,放弃了。

现在有找到个好方法,就是是用128字体来打印。当然还没测试读取的效果,先把方法贴上,明天去测试速度。

有两种转换128码的方式,有待明天测试......

方式A:

 1 private string StringToCode128(string String)
 2         {
 3             try
 4             {
 5                 char endChar = (char)0;
 6                 Int64 total = 104;
 7                 int tmp;
 8                 Int64 endAsc;
 9                 for (int i = 0; i < String.Length; i++)
10                 {
11                     tmp = (int)(Convert.ToChar(String.Substring(i, 1)));
12                     if (tmp >= 32)
13                     {
14                         total += (tmp - 32) * (i + 1);
15                     }
16                     else
17                     {
18                         total += (tmp + 64) * (i + 1);
19                     }
20                 }
21                 endAsc = total % 103;
22                 if (endAsc >= 95)
23                 {
24                     switch (endAsc)
25                     {
26                         case 95:
27                             endChar = '?';
28                             break;
29                         case 96:
30                             endChar = '?';
31                             break;
32                         case 97:
33                             endChar = '?';
34                             break;
35                         case 98:
36                             endChar = '?';
37                             break;
38                         case 99:
39                             endChar = '?';
40                             break;
41                         case 100:
42                             endChar = 'è';
43                             break;
44                         case 101:
45                             endChar = 'é';
46                             break;
47                         case 102:
48                             endChar = 'ê';
49                             break;
50                     }
51                 }
52                 else
53                 {
54                     endAsc += 32;
55                     endChar = (char)endAsc;
56                 }
57                 return "ì" + String + endChar.ToString() + "?";
58 
59             }
60             catch (Exception ex)
61             {
62                 MessageBox.Show("Error:" + ex.Message);
63                 return string.Empty;
64             }
65 
66         }

方法B:

 1 private string Get128CodeString(string inputData)
 2         {
 3             string result;
 4             int checksum = 104;
 5             for (int ii = 0; ii < inputData.Length; ii++)
 6             {
 7                 if (inputData[ii] >= 32)
 8                 {
 9                     checksum += (inputData[ii] - 32) * (ii + 1);
10                 }
11                 else
12                 {
13                     checksum += (inputData[ii] + 64) * (ii + 1);
14                 }
15             }
16             checksum = checksum % 103;
17             if (checksum < 95)
18             {
19                 checksum += 32;
20             }
21             else
22             {
23                 checksum += 100;
24             }
25             result = Convert.ToChar(204) + inputData.ToString() + Convert.ToChar(checksum) + Convert.ToChar(206);
26             return result;
27         }
  Font ft_BarCode = new System.Drawing.Font("Code 128", 43, FontStyle.Regular, GraphicsUnit.World);

  Font ft1 = new System.Drawing.Font("Times New Roman", 14, FontStyle.Regular, GraphicsUnit.World);



gB.DrawString(Get128CodeString(this.gridView1.GetRowCellValue(e.RowHandle, "BARCODE").ToString()),
ft_BarCode, b1, new Point(40, 138));// new Rectangle(40, 138, 230, 40));

gB.DrawString("*"+this.gridView1.GetRowCellValue(e.RowHandle, "BARCODE").ToString()+"*",
new Font(this.Font.FontFamily, 10, FontStyle.Regular), b1, new Point(100, 177));// new Rectangle(80, 180, 230, 20));
原文地址:https://www.cnblogs.com/lzh_527/p/2883749.html