| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- /*******************************************************************************
- * Copyright(c) 2014 dongke All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:BarcodeUtility.cs
- * 2.功能描述:条码打印与条码图片解析
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 陈冰 2014/10/27 1.00 新建
- *******************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using ZXing;
- using ZXing.Common;
- using ZXing.Rendering;
- namespace Dongke.IBOSS.PRD.Basics.Library
- {
- /// <summary>
- /// 条码打印与条码图片解析
- /// </summary>
- public class BarcodePrintUtility
- {
- /// <summary>
- /// 纸张类型
- /// </summary>
- public enum PaperType
- {
- /// <summary>
- /// A4纸张
- /// </summary>
- A4,
- }
- // 采用code128编码方式
- private static BarcodeFormat _barcodeFormat = BarcodeFormat.CODE_128;
- /// <summary>
- /// 条码转换成图片类
- /// </summary>
- /// <param name="barcode">条码</param>
- /// <param name="barcodeHeight">高</param>
- /// <param name="barcodeWidth">宽</param>
- /// <returns></returns>
- public static Image BarcodeConvertImage(string barcode, int barcodeWidth, int barcodeHeight)
- {
- BarcodeWriter barcodeWriter = new BarcodeWriter();
- barcodeWriter.Format = _barcodeFormat;
- BarcodeWriterGeneric<Bitmap> arg_50_0 = barcodeWriter;
- arg_50_0.Options = new EncodingOptions
- {
- Height = barcodeHeight,
- Width = barcodeWidth
- };
- barcodeWriter.Renderer = new BitmapRenderer();
- return barcodeWriter.Write(barcode);
- }
- /// <summary>
- /// 画出A4纸张集合
- /// </summary>
- /// <param name="barcodeList"></param>
- /// <param name="barcodeWidth"></param>
- /// <param name="barcodeHeight"></param>
- /// <param name="paperType"></param>
- /// <returns></returns>
- public static List<Image> BarcodeDrawPaper(List<string> barcodeList, int barcodeWidth, int barcodeHeight, BarcodePrintUtility.PaperType paperType)
- {
- // 纸张的大小
- int paperWidth = 0;
- int paperHeight = 0;
- // 位置让画出的条码上下高度适中 增加10像素。便于观看纸张
- int addHeight = 10;
- if (paperType == BarcodePrintUtility.PaperType.A4)
- {
- paperWidth = 595;
- paperHeight = 842;
- }
- // 条码摆放纸张的列数
- int colCount = paperWidth / barcodeWidth;
- // 条码摆放纸张的行数
- int rowCount = paperHeight / (barcodeHeight + addHeight);
- List<Image> imgList = new List<Image>();
- // 纸张
- Bitmap newBmp = null;
- Graphics g = null;
- for (int i = 0; i < barcodeList.Count; i++)
- {
- // 构建新纸张
- if (i % (colCount * rowCount) == 0)
- {
- newBmp = new Bitmap(paperWidth, paperHeight);
- g = Graphics.FromImage(newBmp);
- // 清空画布,并以白色背景填充
- g.Clear(System.Drawing.Color.White);
- imgList.Add(newBmp);
- }
- // 条码图片
- Image img = BarcodePrintUtility.BarcodeConvertImage(barcodeList[i], barcodeWidth, barcodeHeight);
- // 设置条码构建的位置
- int x = i % (colCount * rowCount) % colCount * barcodeWidth;
- int y = i % (colCount * rowCount) / colCount * (barcodeHeight + addHeight);// 加像素表示 上下有点间距
- g.DrawImage(img, x, y);
- }
- return imgList;
- }
- }
- }
|