BarcodePrintUtility.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*******************************************************************************
  2. * Copyright(c) 2014 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:BarcodeUtility.cs
  5. * 2.功能描述:条码打印与条码图片解析
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈冰 2014/10/27 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Drawing;
  13. using ZXing;
  14. using ZXing.Common;
  15. using ZXing.Rendering;
  16. namespace Dongke.IBOSS.PRD.Basics.Library
  17. {
  18. /// <summary>
  19. /// 条码打印与条码图片解析
  20. /// </summary>
  21. public class BarcodePrintUtility
  22. {
  23. /// <summary>
  24. /// 纸张类型
  25. /// </summary>
  26. public enum PaperType
  27. {
  28. /// <summary>
  29. /// A4纸张
  30. /// </summary>
  31. A4,
  32. }
  33. // 采用code128编码方式
  34. private static BarcodeFormat _barcodeFormat = BarcodeFormat.CODE_128;
  35. /// <summary>
  36. /// 条码转换成图片类
  37. /// </summary>
  38. /// <param name="barcode">条码</param>
  39. /// <param name="barcodeHeight">高</param>
  40. /// <param name="barcodeWidth">宽</param>
  41. /// <returns></returns>
  42. public static Image BarcodeConvertImage(string barcode, int barcodeWidth, int barcodeHeight)
  43. {
  44. BarcodeWriter barcodeWriter = new BarcodeWriter();
  45. barcodeWriter.Format = _barcodeFormat;
  46. BarcodeWriterGeneric<Bitmap> arg_50_0 = barcodeWriter;
  47. arg_50_0.Options = new EncodingOptions
  48. {
  49. Height = barcodeHeight,
  50. Width = barcodeWidth
  51. };
  52. barcodeWriter.Renderer = new BitmapRenderer();
  53. return barcodeWriter.Write(barcode);
  54. }
  55. /// <summary>
  56. /// 画出A4纸张集合
  57. /// </summary>
  58. /// <param name="barcodeList"></param>
  59. /// <param name="barcodeWidth"></param>
  60. /// <param name="barcodeHeight"></param>
  61. /// <param name="paperType"></param>
  62. /// <returns></returns>
  63. public static List<Image> BarcodeDrawPaper(List<string> barcodeList, int barcodeWidth, int barcodeHeight, BarcodePrintUtility.PaperType paperType)
  64. {
  65. // 纸张的大小
  66. int paperWidth = 0;
  67. int paperHeight = 0;
  68. // 位置让画出的条码上下高度适中 增加10像素。便于观看纸张
  69. int addHeight = 10;
  70. if (paperType == BarcodePrintUtility.PaperType.A4)
  71. {
  72. paperWidth = 595;
  73. paperHeight = 842;
  74. }
  75. // 条码摆放纸张的列数
  76. int colCount = paperWidth / barcodeWidth;
  77. // 条码摆放纸张的行数
  78. int rowCount = paperHeight / (barcodeHeight + addHeight);
  79. List<Image> imgList = new List<Image>();
  80. // 纸张
  81. Bitmap newBmp = null;
  82. Graphics g = null;
  83. for (int i = 0; i < barcodeList.Count; i++)
  84. {
  85. // 构建新纸张
  86. if (i % (colCount * rowCount) == 0)
  87. {
  88. newBmp = new Bitmap(paperWidth, paperHeight);
  89. g = Graphics.FromImage(newBmp);
  90. // 清空画布,并以白色背景填充
  91. g.Clear(System.Drawing.Color.White);
  92. imgList.Add(newBmp);
  93. }
  94. // 条码图片
  95. Image img = BarcodePrintUtility.BarcodeConvertImage(barcodeList[i], barcodeWidth, barcodeHeight);
  96. // 设置条码构建的位置
  97. int x = i % (colCount * rowCount) % colCount * barcodeWidth;
  98. int y = i % (colCount * rowCount) / colCount * (barcodeHeight + addHeight);// 加像素表示 上下有点间距
  99. g.DrawImage(img, x, y);
  100. }
  101. return imgList;
  102. }
  103. }
  104. }