转载: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:剩下的就是演示了:本机演示结果如下:

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

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

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

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

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


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

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

  1. C#利用zxing.net生成二维码和条形码并实现打印的功能

    C#利用Zxing.net生成条形码和二维码并实现打印的功能     开篇:zxing.net是.net平台下编解条形码和二维码的工具. 下载地址:http://pan.baidu.com/s/1kT ...

  2. ZXing 生成二维码和条形码

    今天,做项目需要使用条形码扫描枪扫描二维码,以后后续手动生成二维码和条形码.看了一下,同事写的例子以及自己在网上查看了一下源码,至于源码怎么搞的,没看,直接上使用功能! Step1:下载地址:http ...

  3. 一句代码生成二维码,一句代码生成条形码,批量生成二维码和条形码,步骤教学

    生产企业或者物流快递需要用到大量的二维码和条形码,但是要自行编写代码批量生成二维码或者条形码并不容易,涉及的知识面很广. Excel插件<E灵>提供了二维码接口和条形码接口,您只需要一句代 ...

  4. 【Demo】 生成二维码 和 条形码

    为什么80%的码农都做不了架构师?>>>    条形码 和 二维码 对比 一维条形码只是在一个方向(一般是水平方向)表达信息,而在垂直方向则不表达任何信息,其一定的高度通常是为了便于 ...

  5. uniapp生成二维码和条形码

    uniapp生成二维码和条形码 我们做小程序在我的这个页面经常会遇到有生成二维码的需求,那么我们使用tki-barcode和tki-qrcode这两个组件进行实现我们的需求 组件下载地址:" ...

  6. Python 【生成二维码和条形码】

    一.概述 最近在学习Python , 编写了一个能生成二维码和条形码的窗体.利用窗体上的输入Entrance,可以输入需要编码的原始数据,分别点击按钮可以生成二维码和条形码.分别点击窗体上的按钮,预览 ...

  7. Android 生成二维码,条形码,二维码添加logo

    zxing生成二维码 implementation 'com.google.zxing:core:3.3.1' implementation(name: 'zxing-1.0.1', ext: 'aa ...

  8. excel每行按模板导出为一个excel文件,可以指定列文本生成二维码或者条形码

    程序修改了bug,增加了功能.(20220825) 该程序可以把一个excel文件中每行数据按指定模板生成一个单独excel文件. 1. 模板文件为tpl\template.xlsx. 2. 从左侧数 ...

  9. 生成二维码与条形码的各种方法

    二维码 方法1:将网址生成二维码的API调用接口 http://pan.baidu.com/share/qrcode?w=150&h=150&url=http://www.54admi ...

最新文章

  1. optee对std smc的处理的详解
  2. Android官方开发文档Training系列课程中文版:Activity测试之创建单元测试
  3. 53-C++ CH08 01
  4. QML工作笔记-使用QML中的Date将时间戳和指定格式时间互转
  5. python用于声明类的关键字_python的with关键字
  6. 1006. Sign In and Sign Out (25)-PAT甲级真题
  7. 实战:基于Node的控制台记事本开发
  8. MySQL 的主从复制
  9. 【Python爬虫实战】Python实现动态网页爬虫(requests模块)
  10. 计算机专业英语pdf词汇百度盘,计算机专业英语词汇词.pdf
  11. 计算机的网络命令大全,windows常用网络命令图文详解
  12. cdn搭建原理_CDN运行过程四大要点,附技术原理图解,智能DNS解析
  13. java 毛笔字,Photoshop设计唯美大气的毛笔字
  14. Android开发踩坑之旅
  15. 重命名技巧,支持多个文件夹快速重命名
  16. 搭建PHP环境需要安装Apache服务器,遇到的一系列的问题(切记需要用管理权限进入CMD)
  17. iOS swift5 手动导入SnapKit(不用cocoapods)
  18. Oracle Data purge(Oracle一键数据删除)
  19. 黑客又没有工资,那他们靠什么来维持生活?答案说出让人意外!
  20. python基础—3

热门文章

  1. Neo4j 图数据库高级应用系列 / 服务器扩展指南 APOC 8.8 - 图生成 完全图
  2. 小狗钱钱:钱钱的金钱语录
  3. 第三方支付机构是如何产生的?有着怎样的历史背景?它的未来又该如何去走得更加辉煌?
  4. 每当图片传过来时进行对比_每当应用开始使用Mac的网络摄像头时如何获取通知...
  5. 国学大师 master of Chinese culture
  6. “使用达芬奇软件实现Autosar架构:配置和注意事项“
  7. html 未读消息红点,消息未读之点不完的小红点(Node+Websocket)
  8. 物联卡需要实名认证吗?物联网卡实名认证有什么用?
  9. 获取html元素的高,获取HTML元素的高度jQuery
  10. CTP接口开发案例(内附源码)