生成条码(图1)、二维码(图2)图片及打印出二维码标签(图3)效果
图1 图2 图3
DLL说明
BarcodeLib.dll--生成条码图片组件
ThoughtWorks.QRCode.dll--生成二维码图片组件
FNTHEX32.DLL--用于斑马条码打印机打印中文
注:打印机名称在Web.config 文件中
网络共享打印机:\\网络计算机名称或IP\网络打印机名称
本地打印机:打印机名称
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Data;
namespace PrintLib.Printers.Zebra
{
using System = global::System;
using ThoughtWorks.QRCode.Codec;
using System.Drawing;
using System.Drawing.Printing;
public class Printer
{
public string Name;
#region 从Web.config文件中获取打印机名称,如
public Printer()
{
this.Name = System.Configuration.ConfigurationManager.AppSettings["Printer"];
}
public Printer(string name)
{
this.Name = name;
}
#endregion
#region DLL声明
//ZPL
[DllImport(@"FNTHEX32.DLL", CharSet = CharSet.Ansi)]
public static extern int GETFONTHEX(
string chnstr,
string fontname,
string chnname,
int orient,
int height,
int width,
int bold,
int italic,
StringBuilder param1);
//EPL
[DllImport(@"Eltronp.dll", CharSet = CharSet.Ansi)]
public static extern int PrintHZ(int Lpt, //0:LPT1,1 LPT2
int x,
int y,
string HZBuf,
string FontName,
int FontSize,
int FontStyle);
#endregion
#region 指令说明
/**
^XA 开始 ^XZ 结束
^LH起始坐标 ^PR进纸回纸速度 ^MD 对比度
^FO标签左上角坐标 ^XG打印图片参数1图片名称后两个为坐标
^FS标签结束符 ^CI切换国际字体 ^FT坐标 ^FD定义一个字符串
^A定义字体 ^FH十六进制数 ^BY模块化label ^BC条形码128
^PQ打印设置 参数一 打印数量 参数二暂停 参数三重复数量 参数四为Y时表明无暂停
**/
#endregion
#region PrintDocument 打印条码、二维码
public void Print()
{
System.Drawing.Printing.PrintDocument _Document = new System.Drawing.Printing.PrintDocument();
_Document.PrintPage += _Document_PrintPage;
PageSettings pageSet = new PageSettings();
pageSet.Landscape = false;
pageSet.Margins.Top = 0;
pageSet.Margins.Left = 1;
pageSet.PaperSize = new System.Drawing.Printing.PaperSize("小票", 2, 2);
_Document.DefaultPageSettings = pageSet;
_Document.Print();
}
void _Document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float x = 0;
float y = 0;
float width = 300;
float height = 60;
e.Graphics.DrawImage(CreateBarcodeImage("--test--",""), x, y, width, height);
e.HasMorePages = false;
}
#endregion
#region ZPL 打印条码、二维码
/// <summary>
/// 条码打印(标签两列)
/// </summary>
/// <param name="