C#利用Zxing.net生成条形码和二维码并实现打印的功能
开篇:zxing.net是.net平台下编解条形码和二维码的工具。
下载地址: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))
{
位数字");
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:剩下的就是演示了:本机演示结果如下:
》运行程序:点击【生成条形码】,结果如下:
》点击【解码】按钮,结果如下:
》点击《打印》按钮,结果如下:
》点击【生成二维码】按钮,结果如下:
》点击【解码】按钮,结果如下:
》点击【打印】按钮,结果如下:
C#利用Zxing.net生成条形码和二维码并实现打印的功能的更多相关文章
- C#Zxing.net生成条形码和二维码
下载Zxing.net官网:https://archive.codeplex.com/?p=zxingnet 或者去VS程序包下载 封装好的代码: using System; using System ...
- 利用ZXing.Net生成和识别二维码
ZXing.Net:ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库. github:https://github.com/micjahn/ZXing.Net 直接将字符 ...
- C# 利用ZXing.Net来生成条形码和二维码
本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正. 什么是ZXing.Net? ZXing是一个开放源码的,用Java实现的多种格式的 ...
- 使用谷歌Z生成条形码以及二维码
下载地址:http://zxingnet.codeplex.com/ zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便. 首先下载二进制dll文件,引入工程: using Sy ...
- 使用ZXing.Net生成与识别二维码(QR Code)
Google ZXing是目前一个常用的基于Java实现的多种格式的1D/2D条码图像处理库,出于其开源的特性其现在已有多平台版本.比如今天要用到的ZXing.Net就是针对微软.Net平台的版本.使 ...
- ZXing生成条形码、二维码、带logo二维码
采用的是开源的ZXing,Maven配置如下,jar包下载地址,自己选择版本下载,顺便推荐下Maven Repository <!-- https://mvnrepository.com/art ...
- winform生成条形码和二维码(ZXing.Net)
首先在项目添加ZXing.Net. 工具-->Nuget包管理器-->Nuget程序包 在所搜栏输入 ZXing.Net 如下图: 添加完成后会看见: 效果图: 所有代码: /// &l ...
- iOS开发——生成条形码,二维码
- (void)viewDidLoad { [super viewDidLoad]; self.imageView.image = [self generateBarCode:@"15248 ...
- ZXing.net 生成和解析二维码
nuget引用zxing.net包 public partial class Form1 : Form { public Form1() { InitializeComponent(); } priv ...
随机推荐
- Arcgis安装要素
1. ArcGIS安装过程中需将用户名改为计算机名,该计算机名称时需要新建对话框. 2. ArcGIS Server安装过程中要设置ArcGISWebServices用户的读写权限,即设置ASP.NE ...
- Sterling B2B Integrator与SAP交互 - 02 安装配置
系统组成: 1. 服务器OS及硬件: OS: Red Hat Enterprise Linux Server release 6.6 Hardware: Virtual Machine, x86_64 ...
- 【分享】熟练的Java程序员应该掌握哪些技术?
Java程序员应该掌握哪些能力才能算是脱离菜鸟达到熟练的程度? 1.语法:Java程序员必须比较熟悉语法,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息 知道是什么样的语法错误并且知道任 ...
- C#_正则表达式
概述 正则表达式,主要是用符号描述了一类特定的文本(模式).而正则表达式引擎则负责在给定的字符串中,查找到这一特定的文本. 本文主要是列出常用的正则表达式符号,加以归类说明.本文仅仅是快速理解了正则表 ...
- python中列表的常用操作增删改查
1. 列表的概念,列表是一种存储大量数据的存储模型. 2. 列表的特点,列表具有索引的概念,可以通过索引操作列表中的数据.列表中的数据可以进行添加.删除.修改.查询等操作. 3. 列表的基本语法 创建 ...
- Tortoisegit生成SSH密钥一次性输入密码
一.找到Tortoisegit 的安装目录,进入bin目录下,找到puttygen.exe并打开,点击Generate按钮,会看到上面出现绿色滚动条,此时要不停移动鼠标,进度会比较快,完成后,就会看到 ...
- C#易忘点
下面是自己总结的一些C#语言方面用的少容易忘的地方,总结一下,在用的时候看一下.(仅针对本人) 参数数组 定义一个函数,用来取得数字的和,但是数字的个数不确定. 解决方案: 1,定义一个函数,参数传递 ...
- LeetCode-765.情侣牵手
N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并肩坐在一起. 一次交换可选择任意两人,让他们站起来交换座位. 人和座位用 0 到 2N-1 的整 ...
- struts2中的方法的调用
转载:http://blog.csdn.net/hephec/article/details/41808585 在Struts2中方法调用概括起来主要有三种形式: 第一种方式:指定method属性 & ...
- jsp数据库开发
完全卸载mysql数据库图文教程 https://jingyan.baidu.com/article/f96699bbaa8fc1894f3c1b5a.html MySQl:123456 JDBC概述 ...