C#通过条码打印机打印条码(斑马打印机)

很久登陆博客园了,自己刚进入开发这行一年,博客上一直不知道写什么,现在把自己工作中遇到的一些关于使用条码打印机的一些代码,贴出来,希望能给出学者带来点帮助,本人水平有限,写的不好多多包含,具体代码如下:

//此类作用打开打印机的串口

 internal class BarcodePrinter
    {

//端口
        private string fileName;

        public BarcodePrinter(string port)
        {
            this.fileName = port;
        }

        [DllImport("PrinteDll.dll")]
        private static extern int ClosePrinter();

//取得打印命令
        public string GetLabelCommand(string fileName)
        {
            StreamReader reader = new StreamReader(fileName);
            string str = "";
            string str2 = null;
            while ((str2 = reader.ReadLine()) != null)
            {
                str = str + str2;
            }
            reader.Close();
            return str;
        }

        [DllImport("PrinteDll.dll")]
        private static extern int OpenPrinter(string fileName);

//打印条码
        public void PrintLabel(string text)
        {
            if (OpenPrinter(this.fileName) < 0)
            {
                throw new Exception("标签打印:打开 " + this.fileName + " 失败。");
            }
            Write(text);
            ClosePrinter();
        }

        [DllImport("PrinteDll.dll")]
        private static extern int Write(string text);
    }

 

 

//调用

//实例化打印对象

BarcodePrinter v_barcode_printer = new Pack_BarCode.Model.BarcodePrinter(串口);

//ZPL II语言(具体参考ZPL II说明)

string strPrint="^XA
^LH0,0
^FO60,303^GB1100,0,10^FS
^FO550,303^GB0,647,10^FS
^FO120,670^A0,25,40^FDPN:$P^FS
^XZ";

//打印方法.

private void PrintText(string str)

{

     v_barcode_printer ..PrintLabel(strPrint.Replace("$P",str);

}

 

 

posted @ 2009-05-22 21:03  NewSoftsNet  Views(6830)  Comments(6Edit  收藏