C#USB连接斑马条码打印机打印二维码、图片、及中文文字_实战项目中摘选

一、开发前言:开发工具:vs2015案例性质:winfrom案例引用:ThoughtWorks.QRCode.Codec.dll (主要用来生成二维码)具体使用方法可网上自行搜索开发思路:待生成二维码数据——二维码图片——转成ZPL指令/文字——图片——转成图片——转成ZPL指令二、效果图 三、代码块1.winfrom窗体代码using System;using System.Drawing.Printing;namespace DemoPrinter{ /// <
摘要由CSDN通过智能技术生成

一、开发前言:

  1. 开发工具:vs2015
  2. 案例性质:winfrom
  3. 案例引用:ThoughtWorks.QRCode.Codec.dll (主要用来生成二维码)具体使用方法可网上自行搜索
  4. 开发思路:待生成二维码数据——二维码图片——转成ZPL指令/文字——图片——转成图片——转成ZPL指令
    二、效果图
    在这里插入图片描述 三、代码块
    1.winfrom窗体代码
using System;
using System.Drawing.Printing;

namespace DemoPrinter
{
   
    /// <summary>
    /// 选择打印机名称
    /// </summary>
    public partial class FmInstalledPrinters : Form
    {
   
        public FmInstalledPrinters()
        {
   
            InitializeComponent();
        }
        private void FmInstalledPrinters_Load(object sender, EventArgs e)
        {
   
            #region 绑定打印机名称
            foreach (string item in PrinterSettings.InstalledPrinters)//获取安装在本计算机上的所有打印机名称
            {
   
                comboBox_printName.Properties.Items.Add(item);
            }
            if (comboBox_printName.Properties.Items.Contains("ZDesigner GK888t (EPL)"))
            {
   
                comboBox_printName.Text = "ZDesigner GK888t (EPL)";
            }
            #endregion
        }

        /// <summary>
        /// 打印标签按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton_print_Click(object sender, EventArgs e)
        {
   
            if (String.IsNullOrEmpty(splitMaterialBatch.sm_batch_no))
            {
   
                this.Close();
                MessageDialog.ShowError("标签打印数据传送失败,请刷新数据后重新操作!");
            }
            else if (comboBox_printName.Text == "")
            {
   
                MessageDialog.ShowError("请选择打印机!");
            }
            else
            {
   
                string text = "";
                string qRCodeContent = AssembleData(splitMaterialBatch.sm_batch_no, splitMaterialBatch.sm_work_no, splitMaterialBatch.operator_wn, splitMaterialBatch.material_no, splitMaterialBatch.unit, splitMaterialBatch.weight.ToString(), splitMaterialBatch.sm_date.ToString("yyyy-MM-dd"), splitMaterialBatch.expiry_date.ToString("yyyy-MM-dd"), out text);

                //发送打印指令
                if (PrintZPL(qRCodeContent, text))
                {
   
                    this.Close();
                    MessageDialog.ShowError("标签打印指令发送成功!");
                }
                else
                {
   
                    MessageDialog.ShowError("标签打印指令发送失败!");
                }
            }
        }

        /// <summary>
        /// 取消打印按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton_clean_Click(object sender, EventArgs e)
        {
   
            this.Close();
        }

        #region 斑马条码打印机

        #region PrintZPL 发送打印指令
        /// <summary>
        /// 发送打印指令
        /// </summary>
        /// <param name="printContent">打印内容</param>
        private bool PrintZPL(string qRCodeContent, string text)
        {
   
            return new SendPrint().MergeSendPrint(comboBox_printName.Text, , text, textBox1);
        }
        #endregion PrintZPL 发送打印指令

        #region AssembleData 组装待打印数据
        /// <summary>
        /// 组装待打印数据
        /// </summary>
        /// <returns></returns>
        private string AssembleData(out string text)
        {
   
        //组装要打印的数据
            return qRCodeString;
        }
        #endregion AssembleData 组装待打印数据
        
        #endregion 斑马条码打印机
    }
}

2.发送打印指令


using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;

namespace DemoPrinter
{
   
    /// <summary>
    /// 发送打印指令
    /// </summary>
    public class SendPrint
    {
   
        #region MergeSendPrint 给打印机发送打印数据及打印指令
        /// <summary>
        /// 给打印机发送打印数据及打印指令
        /// </summary>
        /// <param name="installedPrinter">连接打印机名称</param>
        /// <param name="QRCodeContent">待生成二维码数据</param>
        /// <param name="text">显示文字数据</param>
        /// <param name="textBox">显示文字数据的font参数传递</param>
        public bool MergeSendPrint(string installedPrinter, string qRCodeContent, string text, TextBox textBox)
        {
   
            StringBuilder sb = new StringBuilder();
            sb.Append("~DGR:SAMPLE.GRF,");

            string printString = CreateQrCode(qRCodeContent);
            string printString1 = CreateTextBitmap(text, textBox
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你想了解如何使用C#编程控制斑马条码打印机打印ZPL格式的标签吗?如果是的话,你可以参考以下步骤: 1. 确保你的斑马条码打印机连接到电脑,并且已正确安装驱动程序。 2. 下载并安装ZebraDesigner软件,该软件可用于创建ZPL格式的标签模板。 3. 在C#项目添加Zebra.Printer.SDK.dll引用。 4. 在代码创建一个ZebraPrinter对象。 5. 使用ZebraDesigner创建一个标签模板,并将其保存为ZPL格式的文件。 6. 在代码打开该文件,并将其的内容发送到打印机。 以下是一个简单的示例代码,用于打印一个包含文本和条码的标签: ``` using System; using System.IO; using Zebra.Printer.SDK; namespace ZPLPrinterDemo { class Program { static void Main(string[] args) { // 创建ZebraPrinter对象 ZebraPrinter printer = new ZebraPrinter(); // 打开标签模板文件 string filePath = @"C:\Labels\LabelTemplate.zpl"; string zplContent = File.ReadAllText(filePath); // 设置打印机参数 printer.ConnectionType = PrinterConnectionType.USB; printer.DeviceName = "Zebra TLP2844"; // 发送ZPL内容到打印机 printer.Send(zplContent); // 关闭打印机连接 printer.Disconnect(); } } } ``` 需要注意的是,上述代码的Zebra.Printer.SDK.dll引用需要从Zebra官网下载,而且该示例假设你的打印机已经连接到电脑并正确安装了驱动程序。如果你的打印机连接方式不同,需要相应地修改打印机参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值