/*******************************************************************************
* 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
{
///
/// 条码打印与条码图片解析
///
public class BarcodePrintUtility
{
///
/// 纸张类型
///
public enum PaperType
{
///
/// A4纸张
///
A4,
}
// 采用code128编码方式
private static BarcodeFormat _barcodeFormat = BarcodeFormat.CODE_128;
///
/// 条码转换成图片类
///
/// 条码
/// 高
/// 宽
///
public static Image BarcodeConvertImage(string barcode, int barcodeWidth, int barcodeHeight)
{
BarcodeWriter barcodeWriter = new BarcodeWriter();
barcodeWriter.Format = _barcodeFormat;
BarcodeWriterGeneric arg_50_0 = barcodeWriter;
arg_50_0.Options = new EncodingOptions
{
Height = barcodeHeight,
Width = barcodeWidth
};
barcodeWriter.Renderer = new BitmapRenderer();
return barcodeWriter.Write(barcode);
}
///
/// 画出A4纸张集合
///
///
///
///
///
///
public static List BarcodeDrawPaper(List 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 imgList = new List();
// 纸张
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;
}
}
}