ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c#使用zxing打印标签

2021-12-15 09:07:19  阅读:168  来源: 互联网

标签:c# 标签 Image writer System zxing new using Drawing


BarCodeClass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZXing.Common;
using ZXing;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;
using ZXing.QrCode;

namespace WindowsFormsApplication1
{
    class BarCodeClass
    {
         ///<summary>
        ///生成条形码
        ///</summary>
        ///<paramname="pictureBox1"></param>
        ///<paramname="Contents"></param>
        public void CreateBarCode(PictureBox pictureBox1, string Contents)
        {
            Regex rg = new Regex("^[0-9]{12}$");
            if (!rg.IsMatch(Contents))
            {
                MessageBox.Show("本例子采用EAN_13编码,需要输入12位数字");
                return;
            }
            EncodingOptions options = null;
            BarcodeWriter writer = null;
            options = new EncodingOptions
            {
                Width = pictureBox1.Width,
                Height = pictureBox1.Height
            };
            writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.ITF;
            writer.Options = options;
            Bitmap bitmap = writer.Write(Contents);
            pictureBox1.Image = bitmap;
        }

        ///<summary>

        ///生成二维码

        ///</summary>

        ///<paramname="pictureBox1"></param>

        ///<paramname="Contents"></param>
        public void CreateQuickMark(PictureBox pictureBox1,string Contents)
        {
            if(Contents == string.Empty)
            {
                MessageBox.Show("输入内容不能为空!");
                return;
            }
            EncodingOptions options =null;
            BarcodeWriter writer =null;
            options = new QrCodeEncodingOptions
           {
               DisableECI = true,
               CharacterSet = "UTF-8",
               Width = pictureBox1.Width,
               Height = pictureBox1.Height
            };
            writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;
            Bitmap bitmap = writer.Write(Contents);
            pictureBox1.Image = bitmap;
        }
        ///<summary>
        ///解码
        ///</summary>
        ///<paramname="pictureBox1"></param>

        public void Decode(PictureBox pictureBox1)
        {
            BarcodeReader reader = new BarcodeReader();
            Result result = reader.Decode((Bitmap)pictureBox1.Image);
        }

    }
}
DocementBase.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class DocementBase : PrintDocument
    {
        //fields
        public Font Font = new Font("Verdana", 10, GraphicsUnit.Point);
        //预览打印
        public DialogResult showPrintPreviewDialog()
        {
            PrintPreviewDialog dialog = new PrintPreviewDialog();
            dialog.Document = this;
            return dialog.ShowDialog();
        }
        //先设置后打印

        public DialogResult ShowPageSettingsDialog()
        {
            PageSetupDialog dialog = new PageSetupDialog();
            dialog.Document = this;
            return dialog.ShowDialog();

        }
    }
}
imageDocument.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;

namespace WindowsFormsApplication1
{
    class imageDocument : DocementBase
    {
        private Image _Image;
        public Image Image
        {
            get
            {
                return _Image;
            }
            set
            {
                _Image = value;
                if (_Image != null)
                {
                    if (_Image.Size.Width > _Image.Size.Height)
                        DefaultPageSettings.Landscape = true;
                    else
                        DefaultPageSettings.Landscape = false;
                }
            }
        }
        public imageDocument()
        {

        }
        public imageDocument(Image image)
        {
            this.Image = image;
        }
        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            if (Image == null)
            {
                throw new InvalidOperationException();
            }
            Rectangle bestFit = GetBestFitRectangle(e.MarginBounds, Image.Size);
            e.Graphics.DrawImage(Image, bestFit);
            e.Graphics.DrawRectangle(Pens.Black, bestFit);
            e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds);
        }
        // 保持高度比:参数为(打印边界的Rectangularle对象,图像大小的Size对象)

        protected Rectangle GetBestFitRectangle(Rectangle toContain, Size objectSize)

        {
            //检查页面是水平还是竖直的。
            bool containerLandscape = false;
            if (toContain.Width > toContain.Height)
                containerLandscape = true;
            //高度比=图像的高/图像的宽
            float aspectRatio = (float)objectSize.Height / (float)objectSize.Width;
            //得到页面左上角的坐标
            int midContainerX = toContain.Left + (toContain.Width / 2);
            int midContainerY = toContain.Top + (toContain.Height / 2);
            int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
            if (containerLandscape == false)
            {
                //竖直图像
                x1 = toContain.Left;
                x2 = toContain.Right;
                //调整之后的height
                int adjustedHeight = (int)((float)toContain.Width * aspectRatio);
                y1 = midContainerY - (adjustedHeight / 2);
                y2 = y1 + adjustedHeight;
            }
            else
            {
                y1 = toContain.Top;
                y2 = toContain.Bottom;
                //调整之后的height
                int adjustedWidth = (int)((float)toContain.Height / aspectRatio);
                x1 = midContainerX - (adjustedWidth / 2);
                x2 = x1 + adjustedWidth;
            }
            return new Rectangle(x1, y1, x2 - x1, y2 - y1);
        }
    }
}
 

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Myprinter();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtMsg.Text = System.DateTime.Now.ToString("yyyyMMddhhmmss").Substring(0, 12);
        }

        /// <summary>
        /// 打印
        /// </summary>
        private void Myprinter()
        {
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(printDocument_PrintA4Page);

            //pd.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GX430t";
            pd.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner 105SLPlus-300dpi ZPL"; //打印机名称
            //pd.DefaultPageSettings.Landscape = true;  //设置横向打印,不设置默认是纵向的
            pd.PrintController = new System.Drawing.Printing.StandardPrintController();
            pd.Print();
        }

        private void printDocument_PrintA4Page(object sender, PrintPageEventArgs e)
        {
            Font titleFont = new Font("黑体", 11, System.Drawing.FontStyle.Bold);//标题字体           
            Font fntTxt = new Font("宋体", 10, System.Drawing.FontStyle.Regular);//正文文字         
            Font fntTxt1 = new Font("宋体", 8, System.Drawing.FontStyle.Regular);//正文文字           
            System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);//画刷           
            System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black);           //线条颜色         

            try
            {
                e.Graphics.DrawString("标题name", titleFont, brush, new System.Drawing.Point(20, 10));

                Point[] points111 = { new Point(20, 28), new Point(230, 28) };
                e.Graphics.DrawLines(pen, points111);

                e.Graphics.DrawString("资产编号:", fntTxt, brush, new System.Drawing.Point(20, 31));
                e.Graphics.DrawString("123456789123465", fntTxt, brush, new System.Drawing.Point(80, 31));
                e.Graphics.DrawString("资产序号:", fntTxt, brush, new System.Drawing.Point(20, 46));
                e.Graphics.DrawString("123456789131321", fntTxt, brush, new System.Drawing.Point(80, 46));

                e.Graphics.DrawString("底部name", fntTxt1, brush, new System.Drawing.Point(100, 62));
                e.Graphics.DrawString("底部name看看字符长度到底多长", fntTxt1, brush, new System.Drawing.Point(240, 62));

                Bitmap bitmap = CreateQRCode("herrisQRCODE202201091530");
                e.Graphics.DrawImage(bitmap, new System.Drawing.Point(20, 72));

            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        /// <summary>
        /// 二维码方法
        /// </summary>
        /// <param name="asset"></param>
        /// <returns></returns>
        public static Bitmap CreateQRCode(string asset)
        {
            EncodingOptions options = new QrCodeEncodingOptions
            {
                DisableECI = true,
                CharacterSet = "UTF-8", //编码
                Width = 100,             //宽度
                Height = 100             //高度
            };
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;
            return writer.Write(asset);
        }
        /// <summary>
        /// 创建条码方法
        /// </summary>
        /// <param name="asset"></param>
        /// <returns></returns>
        public static Bitmap CreateCode(string asset)
        {
            // 1.设置条形码规格
            EncodingOptions options = new EncodingOptions();
            options.Height = 40; // 必须制定高度、宽度
            options.Width = 120;


            // 2.生成条形码图片并保存
            BarcodeWriter writer = new BarcodeWriter();
            writer.Options = options;
            writer.Format = BarcodeFormat.CODE_128;     //二维码编码
            return writer.Write(asset);     // 生成图片
        }
        private BarCodeClass bcc = new BarCodeClass();
        private DocementBase _docement;

        //生成条形码
        private void button2_Click(object sender, EventArgs e)
        {
            bcc.CreateBarCode(pictureBox1, txtMsg.Text);
        }

        //生成二维码
        private void button3_Click(object sender, EventArgs e)
        {
            bcc.CreateQuickMark(pictureBox1, txtMsg.Text);
        }

        //解码
        private void button4_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("请录入图像后再进行解码!");
                return;
            }
            BarcodeReader reader = new BarcodeReader();
            Result result = reader.Decode((Bitmap)pictureBox1.Image);
            MessageBox.Show(result.Text);
        }

        //打印
        private void button5_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("You Must Load an Image first!");
                return;
            }
            else
            {
                _docement = new imageDocument(pictureBox1.Image);
            }
            _docement.showPrintPreviewDialog();
        }
        
    }
}
 

 

标签:c#,标签,Image,writer,System,zxing,new,using,Drawing
来源: https://blog.csdn.net/Aa13451765025/article/details/121944341

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有