基本信息
源码名称:网口无驱打印,采用图片打印模板,不使用esc指令
源码大小:2.03M
文件格式:.rar
开发语言:C#
更新时间:2019-07-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

这是补上的网口无驱打印,串口并口驱动打印在另外一个实例,还有就是这个不是使用esc指令,而是使用整个打印内容先绘制出来图片,再打印图片。esc指令打印和这个图片打印各有好处。看个人喜欢。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using ThoughtWorks.QRCode.Codec;
using ZXing;
using ZXing.Common;

namespace NetPrinterTest2
{
    /// <summary>
    /// 打印行信息
    /// </summary>
    public class PrinterRowUtils
    {
        #region 私有成员

        //80小票打印机,每行48个字符(汉字占2个字符)
        private const int LINE_BYTE_SIZE = 48;              //58打印机一般为32字

        /// <summary>
        /// 创建指定数量的填充字符
        /// </summary>
        /// <param name="fill">要填充的字符</param>
        /// <param name="count">填充字符的个数</param>
        /// <returns></returns>
        private static string GetFillText(char fill, int count)
        {
            string result = "";
            for (int i = 0; i < count; i )
            {
                result = fill;
            }
            return result;
        }
        /// <summary>
        /// 获得二维码图像(使用ThoughtWorks.QRCode.dll生成)
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static Bitmap GetCode2DBmp(string text)
        {
            int len = text.Length;
            if (len > 100)
                return null;

            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeVersion = len < 42 ? 3 : len < 62 ? 4 : len < 84 ? 5 : 6;
            qrCodeEncoder.QRCodeScale = 6;
            Bitmap bmp = qrCodeEncoder.Encode(text);
            return bmp;
        }

        /// <summary>
        /// 创建条形码图像(使用zxing.dll生成)
        /// </summary>
        /// <param name="text">14位数字</param>
        /// <returns></returns>
        public static Bitmap GetCode1DBmp(string text)
        {
            text = text.Trim();

            foreach (char c in text)
            {
                if (!char.IsNumber(c))
                    return null;
            }

            if (text.Length < 10 || text.Length > 18 || text.Length % 2 != 0)
                return null;

            EncodingOptions options = null;
            BarcodeWriter writer = null;
            options = new EncodingOptions()
            {
                Width = 300,
                Height = 100
            };
            writer = new BarcodeWriter();

            //【ITF码规则:[取值范围:数字] [字符数量2-254(长度必须为偶数)]】
            writer.Format = BarcodeFormat.ITF;          
            writer.Options = options;

            Bitmap bitmap = writer.Write(text);
            Graphics gr = Graphics.FromImage(bitmap);
            gr.DrawImage(bitmap, 0, 30);

            return bitmap;
        }
        #endregion

        /// <summary>
        /// 获得小票头部
        /// </summary>
        /// <param name="logo">Logo标识</param>
        /// <param name="orderNo">订单编号</param>
        /// <param name="shopName">分店名称</param>
        /// <returns></returns>
        public static byte[] GetHeadRow(Bitmap logo, string orderNo, string shopName)
        {
            Font f1 = new Font("微软雅黑", 20);
            Font f2 = new Font("宋体", 20, FontStyle.Bold);
            Brush b = Brushes.Black;

            //创建小票头部位图(宽度=48字符*12像素=576字节)
            Bitmap bmp = new Bitmap(LINE_BYTE_SIZE * 12, 120);
            Graphics g = Graphics.FromImage(bmp);

            //填充为白色
            g.FillRectangle(Brushes.White, 0, 0, LINE_BYTE_SIZE * 12, 120);
            //绘制一条直线
            g.DrawLine(Pens.Black, 0, 110, LINE_BYTE_SIZE * 12, 110);

            //绘制LOGO
            g.DrawImage(logo, new PointF(0, 0));

            //绘制单号
            Matrix matrix = new Matrix();
            matrix.Scale(1.0f, 3.0f);
            g.Transform = matrix;
            g.DrawString(orderNo, f1, b, new PointF(250, 0));

            //绘制店名
            matrix = new Matrix();
            matrix.Scale(1.0f, 1.5f);
            g.Transform = matrix;
            g.DrawString(shopName, f2, b, new PointF(458, 35));

            return PrinterCmdUtils.bmpToByte(bmp);
        }

        /// <summary>
        /// 绘制直线
        /// </summary>
        /// <returns></returns>
        public static byte[] GetLineRow()
        {
            //使用位图绘制直线
            Bitmap bmp = new Bitmap(LINE_BYTE_SIZE * 12, 11);
            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(Brushes.White, 0, 0, LINE_BYTE_SIZE * 12, 5);
            g.FillRectangle(Brushes.White, 0, 6, LINE_BYTE_SIZE * 12, 5);
            return PrinterCmdUtils.bmpToByte(bmp);
        }

        /// <summary>
        /// 获得条形码的行(左边显示文字,右边显示条码)
        /// </summary>
        /// <param name="leftText"></param>
        /// <param name="code1d"></param>
        /// <returns></returns>
        public static byte[] GetCode1D(string leftText, string code1d)
        {
            Bitmap bmp = new Bitmap(LINE_BYTE_SIZE * 12, 140);
            Bitmap codeImage = GetCode1DBmp(code1d);

            Font f = new Font("宋体", 20, FontStyle.Bold);
            Brush b = Brushes.Black;

            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(Brushes.White, 0, 0, LINE_BYTE_SIZE * 12, 140);
            g.DrawLine(Pens.Black, 0, 20, LINE_BYTE_SIZE * 12, 20);
            g.DrawImage(codeImage, new PointF(276, 25));
            g.DrawLine(Pens.Black, 0, 130, LINE_BYTE_SIZE * 12, 130);

            Matrix matrix = new Matrix();
            matrix.Scale(1.0f, 1.5f);
            g.Transform = matrix;
            g.DrawString(leftText, f, b, new PointF(0, 35));

            return PrinterCmdUtils.bmpToByte(bmp);
        }

        internal static byte[] GetText(string text)
        {
            return System.Text.Encoding.GetEncoding("gbk").GetBytes(text);
        }

        /// <summary>
        /// 获得左右布局的文本
        /// </summary>
        /// <param name="leftText">左边的文本</param>
        /// <param name="rightText">右边的文本</param>
        /// <returns></returns>
        public static byte[] GetLRText(string leftText, string rightText)
        {
            //左边字符长度
            int leftCount = Encoding.Default.GetByteCount(leftText);
            //右边字符长度
            int rightCount = Encoding.Default.GetByteCount(rightText);
            //中间填充空格的数量
            int fillCount = LINE_BYTE_SIZE - leftCount - rightCount;

            string result = leftText GetFillText(' ', fillCount) rightText;
            return System.Text.Encoding.GetEncoding("gbk").GetBytes(result);
        }

        /// <summary>
        /// 获得二维码码的行(右边显示文字,左边显示二维码)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="rightText"></param>
        /// <returns></returns>
        public static byte[] GetCode2D(string url, string rightText)
        {
            Bitmap c2d = GetCode2DBmp(url);
            Font f = new Font("宋体", 18, FontStyle.Bold);

            Bitmap bmp = new Bitmap(LINE_BYTE_SIZE * 12, c2d.Height 20);
            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(Brushes.White, 0, 0, LINE_BYTE_SIZE * 12, 250);
            g.DrawImage(c2d, 10, 10, c2d.Width, c2d.Height);
            g.DrawString(rightText, f, Brushes.Black, new PointF(c2d.Width 20, 10));

            return PrinterCmdUtils.bmpToByte(bmp);
        }

        /// <summary>
        /// 销售商品信息
        /// </summary>
        /// <param name="name">商品名称</param>
        /// <param name="count">数量</param>
        /// <param name="price">单价</param>
        /// <returns></returns>
        public static byte[] GetSellsText(string name, int count, decimal price)
        {
            string leftText = name " ";
            //数量 != 1 时,使用 '-' 填充空白部分
            char fill = count == 1 ? ' ' : '-';

            //单价文本
            string priceText = price.ToString("N2");
            //右边的文本=数量 单价
            string rightText = " x" count GetFillText(' ', 7 - priceText.Length) priceText;

            //计算填充的字符数
            int leftCount = Encoding.Default.GetByteCount(leftText);
            int rightCount = Encoding.Default.GetByteCount(rightText);
            int fillCount = 0;
            int rowCount = 0;
            while (true)
            {
                rowCount ;

                int tcount = rowCount * LINE_BYTE_SIZE;
                if (tcount > leftCount rightCount)
                {
                    fillCount = tcount - leftCount - rightCount;
                    break;
                }
            }

            //盘点每行最后一个字符是否是跨行中文,如果是,填充则少一个字符
            int jc = 0;
            int i = 0;
            foreach (char c in leftText)
            {
                int t = Encoding.Default.GetByteCount(c.ToString());
                if (i % LINE_BYTE_SIZE == LINE_BYTE_SIZE - 1 && t == 2)
                    jc ;

                i = t;
            }
            fillCount -= jc;

            //连接商品名称,中间填充的字符数量和单价
            string result = leftText GetFillText(fill, fillCount) rightText;
            return System.Text.Encoding.GetEncoding("gbk").GetBytes(result);
        }
    }
}