C#生成二维码和条形码并实现打印的功能

article/2025/8/26 3:27:10

转载:https://www.cnblogs.com/xixim/p/4589078.html

下载地址:http://pan.baidu.com/s/1kTr3Vuf

Step1:使用VS2010新建一个窗体程序项目:

Step2:添加三个类:分别是BarCodeClass.cs、DocementBase.cs、imageDocument.cs。(下一步贴出这些类的代码);;;;添加下载回来的引用zxing.dll。

》说明:

《1》   BarCodeClass.cs主要用来实现条形码和二维码的生成和解析。

《2》   DocementBase.cs、imageDocument.cs这两个类是用来实现对生成的条形码和二维码进行打印。

Step3:编写上一步的三个类的代码:

》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 BarCode

{

    class BarCodeClass

    {

        ///<summary>

        ///生成条形码

        ///</summary>

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

        ///<paramname="Contents"></param>

        public void CreateBarCode(PictureBoxpictureBox1,string Contents)

        {

            Regexrg = new Regex("^[0-9]{12}$");

            if(!rg.IsMatch(Contents))

             {

                 MessageBox.Show("本例子采用EAN_13编码,需要输入12位数字");

                 return;

             }

 

            EncodingOptionsoptions =null;

            BarcodeWriterwriter =null;

            options = newEncodingOptions

            {

                Width = pictureBox1.Width,

                Height = pictureBox1.Height

            };

            writer = newBarcodeWriter();

            writer.Format = BarcodeFormat.ITF;

            writer.Options = options;

 

            Bitmapbitmap = writer.Write(Contents);

            pictureBox1.Image = bitmap; 

        }

 

        ///<summary>

        ///生成二维码

        ///</summary>

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

        ///<paramname="Contents"></param>

        public void CreateQuickMark(PictureBoxpictureBox1,string Contents)

        {

            if(Contents == string.Empty)

            {

                MessageBox.Show("输入内容不能为空!");

                return;

            }

 

            EncodingOptionsoptions =null;

            BarcodeWriterwriter =null;

 

            options = newQrCodeEncodingOptions

           {

               DisableECI = true,

               CharacterSet = "UTF-8",

               Width = pictureBox1.Width,

               Height = pictureBox1.Height

           };

            writer = newBarcodeWriter();

            writer.Format = BarcodeFormat.QR_CODE;

            writer.Options = options;

 

        

            Bitmapbitmap = writer.Write(Contents);

            pictureBox1.Image = bitmap;

        }

 

        ///<summary>

        ///解码

        ///</summary>

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

        public void Decode(PictureBoxpictureBox1)

        {

            BarcodeReaderreader =new BarcodeReader();

            Resultresult = 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 BarCode

{

    class DocementBase : PrintDocument

    {

        //fields

        public Font Font = new Font("Verdana",10, GraphicsUnit.Point);

 

        //预览打印

        public DialogResult showPrintPreviewDialog()

        {

            PrintPreviewDialogdialog =new PrintPreviewDialog();

            dialog.Document = this;

 

            returndialog.ShowDialog();

        }

 

        //先设置后打印

        public DialogResult ShowPageSettingsDialog()

        {

            PageSetupDialogdialog =new PageSetupDialog();

            dialog.Document = this;

 

            returndialog.ShowDialog();

        }

    }

}

 

》imageDocument.cs

    using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

using System.Drawing.Printing;

 

namespace BarCode

{

    class imageDocument : DocementBase

    {

        privateImage _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;

                }

            }

        }

 

        publicimageDocument()

        {

 

        }

 

        publicimageDocument(Image image)

        {

            this.Image= image;

        }

 

        protectedoverridevoidOnPrintPage(PrintPageEventArgs e)

        {

            if(Image == null)

            {

                thrownewInvalidOperationException();

            }

 

            RectanglebestFit = 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对象)

        protectedRectangle GetBestFitRectangle(Rectangle toContain,SizeobjectSize)

        {

            //检查页面是水平还是竖直的。

            boolcontainerLandscape =false;

            if(toContain.Width > toContain.Height)

                containerLandscape = true;

 

            //高度比=图像的高/图像的宽

            floataspectRatio = (float)objectSize.Height / (float)objectSize.Width;

            //得到页面左上角的坐标

            intmidContainerX = toContain.Left + (toContain.Width / 2);

            intmidContainerY = toContain.Top + (toContain.Height / 2);

 

            intx1 = 0, x2 = 0, y1 = 0, y2 = 0;

            if(containerLandscape ==false)

            {

                //竖直图像

                x1 = toContain.Left;

                x2 = toContain.Right;

                //调整之后的height

                intadjustedHeight = (int)((float)toContain.Width * aspectRatio);

 

                y1 = midContainerY -(adjustedHeight / 2);

                y2 = y1 + adjustedHeight;

            }

            else

            {

                y1 = toContain.Top;

                y2 = toContain.Bottom;

                //调整之后的height

                intadjustedWidth = (int)((float)toContain.Height/ aspectRatio);

 

                x1 = midContainerX -(adjustedWidth / 2);

                x2 = x1 + adjustedWidth;

            }

            returnnewRectangle(x1,y1, x2 - x1, y2 - y1);

        }

    }

}

 

Step4:修改界面。

Step5:依次双击【生成条形码】、【生成二维码】、【解码】、【打印】等按钮,进入Click事件,编写后台代码。这里不再一一讲述如何实现。代码参照下一步:

 

Step6:贴出窗体的全部代码。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Text.RegularExpressions;

using ZXing;

using ZXing.QrCode.Internal;

using ZXing.Common;

using System.IO;

using ZXing.QrCode;

 

namespace BarCode

{

    public partial class Main : Form

    {

        publicMain()

        {

            InitializeComponent(); 

        }

 

       private BarCodeClass bcc = newBarCodeClass();

       private DocementBase _docement;

 

        //生成条形码

        privatevoid button1_Click(objectsender,EventArgs e)

        {

 

            bcc.CreateBarCode(pictureBox1,txtMsg.Text);

          

        }

        //生成二维码

        privatevoid button2_Click(objectsender,EventArgs e)

        {

            bcc.CreateQuickMark(pictureBox1, txtMsg.Text);

        }

 

        privatevoid Form1_Load(objectsender,EventArgs e)

        {

            txtMsg.Text = System.DateTime.Now.ToString("yyyyMMddhhmmss").Substring(0,12);

        }

        //解码

        privatevoid button4_Click(objectsender,EventArgs e)

        {

            if(pictureBox1.Image ==null)

            {

                MessageBox.Show("请录入图像后再进行解码!");

                return;

            }

            BarcodeReaderreader =new BarcodeReader(); 

            Resultresult = reader.Decode((Bitmap)pictureBox1.Image);

            MessageBox.Show(result.Text);

        }

 

        //打印

        privatevoid button3_Click(objectsender,EventArgs e)

        {

          

            if(pictureBox1.Image ==null)

            {

                MessageBox.Show("You Must Load an Image first!");

                return;

            }

            else

            {

                _docement=new imageDocument(pictureBox1.Image);

            }

          _docement.showPrintPreviewDialog();

        }

    }

 

}

 

Step7:剩下的就是演示了:本机演示结果如下:

》运行程序:点击【生成条形码】,结果如下:

》点击【解码】按钮,结果如下:

》点击《打印》按钮,结果如下:

》点击【生成二维码】按钮,结果如下:

》点击【解码】按钮,结果如下:


》点击【打印】按钮,结果如下:


http://chatgpt.dhexx.cn/article/eIn8LkOe.shtml

相关文章

web项目js调用斑马打印机打印二维码

斑马打印机打印二维码 项目&#xff08;Web项目&#xff09;功能中存在生成并打印二维码的功能&#xff0c;需要借助打印机打印出二维码。由于业务需求二维码需要打印在不干胶的材料上并可以进行粘贴&#xff0c;所以借助斑马打印机通过热敏不干胶纸进行打印。 需要结合所使用…

python实现扫描二维码并打印数据

编译环境 1、操作系统&#xff1a;windows 10 2、语言&#xff1a;python 3.7.0 3、编辑器&#xff1a;pycharm-community-2018.2.8 4、需要提前安装的库&#xff1a;pyzbar、opencv-python 代码 # 首先导入本次所需要的库&#xff0c;最后一个csv是Python自带的csv表格操…

uni-app H5+ 连接蓝牙打印机打印文字及二维码

基于Native.js 实现的连接蓝牙打印机 打印效果图核心代码测试代码运行设备及环境PS&#xff1a;PPS&#xff1a;Demo 打印效果图 核心代码 /*** Description: 蓝牙打印类 基于h5 Native.js* Author: EricLee* Date: 2020-10-14 13:53:23* Param: mod* Return: $*/export cons…

5,uniapp功能之—打印机,打印文本和二维码等,(佳博的打印机)

效果图&#xff1a; 思路&#xff1a;1&#xff0c;一个打印按钮&#xff0c;2点击按钮弹窗一个蓝牙模块&#xff0c;选择蓝牙进行连接&#xff0c;连接成功就直接进行打印了&#xff0c;3&#xff0c;打印的内容自己设置 项目结构&#xff1a; 上代码&#xff0c;打印机参数…

批量生成二维码、打印

推荐使用GoodMES云标签打印&#xff0c;地址为&#xff1a;https://t.goodmes.com/home 1、登陆后点击设计与打印——>数据表 2、需要下载云标签打印 3、下载后进行登陆 4、登陆后无需管它&#xff0c;放在那运行即可 5、这个时候你的云标签助手就会现在在线状态 6、点…

electron打印条形码、二维码

功能&#xff1a;在electron打印条形码或者二维码 在百度搜索找不到合适的博客与插件&#xff0c;就在github上找到一个插件 electron-pos-printer 可以一键打印图片、文本、二维码、条形码、表格 不用安装其他多余插件&#xff0c;亲测十分好用 1、安装 npm install ele…

佳博打印机打印条码和二维码的方法

最近项目需求连接蓝牙打印机打印小票 其中小票中有条码和二维码&#xff0c;很多朋友刚接触打印接的打印逻辑&#xff0c;看不太明白&#xff0c;因此我之前给各位提供现成的打印方法&#xff1a; 关键就是安装打印的文档 提前写好各种打印的调用发放&#xff0c;直接在打印模…

【C#】简单二维码制作和打印

系列文章 【C#】条码管理操作手册 本文链接&#xff1a;https://blog.csdn.net/youcheng_ge/article/details/126589496 【C#】IIS平台下&#xff0c;WebAPI发布及异常处理 本文链接&#xff1a;https://blog.csdn.net/youcheng_ge/article/details/126539836 【C#】简单二维…

一维码二维码的生成及打印

VUE条形码及二维码的生成及输出到打印机 文章目录 VUE条形码及二维码的生成及输出到打印机1. 条形码的生成2. 二维码的生成3. 输出打印4. 将代码提取一下 1. 条形码的生成 安装插件 npm install jsbarcode --save github地址&#xff1a; https://github.com/lindell/JsBarcode…

C#二维码的生成及打印

窗体设计显示图 一&#xff0c;生成二维码 /// <summary>/// 生成二维码/// </summary>/// <param name"text">内容</param>/// <param name"width">宽度</param>/// <param name"height">高度</p…

生成二维码,并且打印

生成二维码&#xff0c;并且打印出来 加入依赖 //二维码依赖 npm install --save qrcodejs2 //打印依赖 npm install vue-print-nb --save在mian文件夹中使用引入并使用&#xff0c;或者按需引入 //打印依赖 import Print from vue-print-nb Vue.use(Print)本次二维码是按需引…

二维码打印

1. 二维码简介 二维码&#xff08;2-dimensional bar code&#xff09;&#xff0c;又称二维条码&#xff0c;它是用特定的几何图形按一定规律在平面&#xff08;二维方向&#xff09;上分布的黑白相间的图形&#xff0c;是所有信息数据的一把钥匙。在现代商业活动中&…

vue生成条形码和二维码并打印

文章目录 前言一、生成条形码二、生成二维码三、效果图四、打印 前言 最近有一个需求&#xff0c;需要将产品信息生成标签&#xff0c;每个信息生成一个条形码&#xff0c;拿到所有数据生成二维码&#xff0c;最后打印标签。 一、生成条形码 使用jsbarcode&#xff0c;直接in…

vue二维码生成、打印及识别

生成二维码 引入QRCode模块 npm install --save qrcode import QRCode from qrcode; 页面代码 批量创建二维码及打印&#xff0c;所以加了个v-for <div id"printDiv"/**后面打印用到的区域id*/ :disabled"hussar_20Disabled" ref"hussar_20Ref&quo…

如何将内网ip映射到外网

这个百度经验上就有&#xff0c;不过对于着急使用的我来说&#xff0c;差了最后一步。所以还是一起记上吧。 第一步 首先登陆你的路由器&#xff0c;就改密码那个。一般就用浏览器直接打开192.168.1.1就可以&#xff0c;账号密码有可能是 admin admin吧&#xff0c;默认可能是…

VMWare虚拟机局域网网络配置,主机访问虚拟机上的网站(端口映射)

文章目录 1 problem2 背景知识3 配置主机网络4 配置虚拟机网络 1 problem 如题&#xff0c;现在虚拟机的80端口和8080端口都有一个网站&#xff0c;我们想要在主机访问它。 2 背景知识 VmWare支持3种网络连接模式。 1、桥接模式&#xff1a; 所谓桥接就是把两个本来分开…

映射公网的几种方式

转载自&#xff1a;http://blog.csdn.net/sadshen/article/details/48240519 这篇文章花了好几天&#xff0c;系统地梳理出了映射公网的几种方式。虽然是针对微信开发的外网服务器来寻找解决方案&#xff0c;但这个知识梳理可能会在其他地方也受益。平常我也有用TeamViewer&…

VMware NAT端口映射 外网可以访问内网虚拟机

我想尝试的是利用本机的ipport来访问虚拟机上的web服务器&#xff0c;因为这样的话&#xff0c;我就能够将我的web服务器部署成为一个能让外网访问的服务器了&#xff0c;首先说下我的环境&#xff1a; 主机&#xff1a;系统win7&#xff0c;ip地址172.18.186.210 虚拟机版本是…

如何把内网IP映射到公网IP

鸽子出品 2017-12-05 22:28:22 我们讲了如何搭建网站&#xff0c;可是有很多小伙伴私信跟我说怎么映射&#xff0c;今天我就教大家如何把内网地址映射到公网&#xff01; 我们所需要的工具有&#xff1a; 内网IP&#xff08;这个是品&#xff0c;也是必有的&#xff01;&…

简单内网映射到公网方法--免费

我是一名Android API Player&#xff0c;最近公司需要做微信公众号二次开发&#xff0c;我跟着学学&#xff0c;公司后台.net。 我mac安装windows之后用vs感觉太差了&#xff0c;可能是我的mac要淘汰了吧。 所以我决定用java后台来跟着做。 仔细一想我没有服务器啊。 再仔细…