开篇: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生成条形码和二维码并实现打印的功能的更多相关文章

  1. C#Zxing.net生成条形码和二维码

    下载Zxing.net官网:https://archive.codeplex.com/?p=zxingnet 或者去VS程序包下载 封装好的代码: using System; using System ...

  2. 利用ZXing.Net生成和识别二维码

    ZXing.Net:ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库. github:https://github.com/micjahn/ZXing.Net 直接将字符 ...

  3. C# 利用ZXing.Net来生成条形码和二维码

    本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正. 什么是ZXing.Net? ZXing是一个开放源码的,用Java实现的多种格式的 ...

  4. 使用谷歌Z生成条形码以及二维码

    下载地址:http://zxingnet.codeplex.com/ zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便. 首先下载二进制dll文件,引入工程: using Sy ...

  5. 使用ZXing.Net生成与识别二维码(QR Code)

    Google ZXing是目前一个常用的基于Java实现的多种格式的1D/2D条码图像处理库,出于其开源的特性其现在已有多平台版本.比如今天要用到的ZXing.Net就是针对微软.Net平台的版本.使 ...

  6. ZXing生成条形码、二维码、带logo二维码

    采用的是开源的ZXing,Maven配置如下,jar包下载地址,自己选择版本下载,顺便推荐下Maven Repository <!-- https://mvnrepository.com/art ...

  7. winform生成条形码和二维码(ZXing.Net)

    首先在项目添加ZXing.Net. 工具-->Nuget包管理器-->Nuget程序包  在所搜栏输入 ZXing.Net 如下图: 添加完成后会看见: 效果图: 所有代码: /// &l ...

  8. iOS开发——生成条形码,二维码

    - (void)viewDidLoad { [super viewDidLoad]; self.imageView.image = [self generateBarCode:@"15248 ...

  9. ZXing.net 生成和解析二维码

    nuget引用zxing.net包 public partial class Form1 : Form { public Form1() { InitializeComponent(); } priv ...

随机推荐

  1. Promise 执行顺序

    加深印象 setTimeout(function() { console.log("timeout-start"); }, 200) // 改为100呢 console.log(& ...

  2. stl源码剖析 详细学习笔记 算法(2)

    //---------------------------15/03/29---------------------------- //****************************set相 ...

  3. winform 记事本 剪切 粘贴 全选 撤销

    private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Undo(); } private voi ...

  4. JavaScript 为什么不要使用 eval

    本文内容 eval 隐藏的 eval 安全问题 结论 参考资料   eval eval 函数是一个高等级的函数,它与任何对象都无关.其参数,如果是一个字符串表达式,那么该函数计算表达式的值:如果是一个 ...

  5. 最简单的iOS网络请求

    做iOS开发,说到网络请求,大家可能都不约而同的提到AFN,可以说大家的网络请求都是用AFN封装而成,AFN的强大易用的确很好. 但是版本升级就会出现一些问题,所以就自己基于iOS原生封装了一个网络请 ...

  6. Python+opencv 图像拼接

    1.http://www.cnblogs.com/skyfsm/p/7411961.html ,给出了很好地拼接算法实现 2.由于不是Python的,所以简单做了一些翻译转成Python+opencv ...

  7. java 中的内部类总结

    内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类. 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液.跳动). 显 ...

  8. 启动Tomcat 卡在 Initializing Spring FrameworkServlet 'SpringMVC'

    使用Myeclipse进行项目开发时莫名其妙启动项目卡在 Initializing Spring FrameworkServlet 'SpringMVC' ,然后等待几分钟后项目才启动起来. 在之前也 ...

  9. 2-Eleventh Scrum Meeting20151211

    第二阶段任务分工整理会议 1.会议任务: (1)明晰第二阶段的开发内容,统计未完成的功能留需完善开发. (2)安排任务分工,每个人的工作安排. (3)PM职位担任. (4)博客内容负责. 2.会议记录 ...

  10. SE Springer小组《Spring音乐播放器》软件需求说明之四

    4 运行环境规定 4.1设备 我们计划完成的音乐软件较小巧,功能并不复杂,在普通笔记本电脑中即可运行,并无特殊硬设备要求. 4.2支持软件 需要用到windows操作系统,用VS编写C/C++代码,还 ...