在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例常用C#方法 → 网口无驱打印,采用图片打印模板,不使用esc指令

网口无驱打印,采用图片打印模板,不使用esc指令

常用C#方法

下载此实例
  • 开发语言:C#
  • 实例大小:2.03M
  • 下载次数:38
  • 浏览次数:506
  • 发布时间:2019-07-18
  • 实例类别:常用C#方法
  • 发 布 人:曾子聪
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 指令 模板 网口打印 图片

实例介绍

【实例简介】

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

【实例截图】

from clipboard

【核心代码】

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);
        }
    }
}


实例下载地址

网口无驱打印,采用图片打印模板,不使用esc指令

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警