欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用.NET如何生成二维码

程序员文章站 2023-12-19 18:42:28
下面介绍一下如何用.net生成二维码(qr code码制),下面给出详细步骤: 1、新建一个window应用程序,然后引入.net二维码类库(开源的类库,可从网上下载):...

下面介绍一下如何用.net生成二维码(qr code码制),下面给出详细步骤:

1、新建一个window应用程序,然后引入.net二维码类库(开源的类库,可从网上下载):

用.NET如何生成二维码

2、构建ui界面,代码如下:

namespace winqrcode 
{ 
  partial class form1 
  { 
    /// <summary> 
    /// 必需的设计器变量。 
    /// </summary> 
    private system.componentmodel.icontainer components = null; 
 
    /// <summary> 
    /// 清理所有正在使用的资源。 
    /// </summary> 
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 
    protected override void dispose(bool disposing) 
    { 
      if (disposing && (components != null)) 
      { 
        components.dispose(); 
      } 
      base.dispose(disposing); 
    } 
 
    #region windows 窗体设计器生成的代码 
 
    /// <summary> 
    /// 设计器支持所需的方法 - 不要 
    /// 使用代码编辑器修改此方法的内容。 
    /// </summary> 
    private void initializecomponent() 
    { 
      this.button1 = new system.windows.forms.button(); 
      this.button2 = new system.windows.forms.button(); 
      this.picturebox1 = new system.windows.forms.picturebox(); 
      this.label1 = new system.windows.forms.label(); 
      this.textbox1 = new system.windows.forms.textbox(); 
      this.label2 = new system.windows.forms.label(); 
      this.lbl解码信息 = new system.windows.forms.label(); 
      ((system.componentmodel.isupportinitialize)(this.picturebox1)).begininit(); 
      this.suspendlayout(); 
      // 
      // button1 
      // 
      this.button1.location = new system.drawing.point(446, 31); 
      this.button1.margin = new system.windows.forms.padding(4, 5, 4, 5); 
      this.button1.name = "button1"; 
      this.button1.size = new system.drawing.size(103, 37); 
      this.button1.tabindex = 0; 
      this.button1.text = "编码"; 
      this.button1.usevisualstylebackcolor = true; 
      this.button1.click += new system.eventhandler(this.button1_click); 
      // 
      // button2 
      // 
      this.button2.location = new system.drawing.point(446, 81); 
      this.button2.margin = new system.windows.forms.padding(4, 5, 4, 5); 
      this.button2.name = "button2"; 
      this.button2.size = new system.drawing.size(103, 37); 
      this.button2.tabindex = 1; 
      this.button2.text = "解码"; 
      this.button2.usevisualstylebackcolor = true; 
      this.button2.click += new system.eventhandler(this.button2_click); 
      // 
      // picturebox1 
      // 
      this.picturebox1.location = new system.drawing.point(23, 117); 
      this.picturebox1.margin = new system.windows.forms.padding(4, 5, 4, 5); 
      this.picturebox1.name = "picturebox1"; 
      this.picturebox1.size = new system.drawing.size(279, 253); 
      this.picturebox1.sizemode = system.windows.forms.pictureboxsizemode.stretchimage; 
      this.picturebox1.tabindex = 2; 
      this.picturebox1.tabstop = false; 
      // 
      // label1 
      // 
      this.label1.autosize = true; 
      this.label1.location = new system.drawing.point(18, 39); 
      this.label1.margin = new system.windows.forms.padding(4, 0, 4, 0); 
      this.label1.name = "label1"; 
      this.label1.size = new system.drawing.size(69, 25); 
      this.label1.tabindex = 3; 
      this.label1.text = "信息:"; 
      // 
      // textbox1 
      // 
      this.textbox1.location = new system.drawing.point(95, 37); 
      this.textbox1.margin = new system.windows.forms.padding(4, 5, 4, 5); 
      this.textbox1.name = "textbox1"; 
      this.textbox1.size = new system.drawing.size(333, 31); 
      this.textbox1.tabindex = 4; 
      // 
      // label2 
      // 
      this.label2.autosize = true; 
      this.label2.location = new system.drawing.point(18, 81); 
      this.label2.margin = new system.windows.forms.padding(4, 0, 4, 0); 
      this.label2.name = "label2"; 
      this.label2.size = new system.drawing.size(88, 25); 
      this.label2.tabindex = 5; 
      this.label2.text = "二维码:"; 
      // 
      // lbl解码信息 
      // 
      this.lbl解码信息.autosize = true; 
      this.lbl解码信息.location = new system.drawing.point(101, 81); 
      this.lbl解码信息.margin = new system.windows.forms.padding(4, 0, 4, 0); 
      this.lbl解码信息.name = "lbl解码信息"; 
      this.lbl解码信息.size = new system.drawing.size(0, 25); 
      this.lbl解码信息.tabindex = 6; 
      // 
      // form1 
      // 
      this.autoscaledimensions = new system.drawing.sizef(11f, 24f); 
      this.autoscalemode = system.windows.forms.autoscalemode.font; 
      this.clientsize = new system.drawing.size(572, 384); 
      this.controls.add(this.lbl解码信息); 
      this.controls.add(this.label2); 
      this.controls.add(this.textbox1); 
      this.controls.add(this.label1); 
      this.controls.add(this.picturebox1); 
      this.controls.add(this.button2); 
      this.controls.add(this.button1); 
      this.font = new system.drawing.font("微软雅黑", 10.8f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((byte)(134))); 
      this.margin = new system.windows.forms.padding(4, 5, 4, 5); 
      this.name = "form1"; 
      this.text = "二维码"; 
      ((system.componentmodel.isupportinitialize)(this.picturebox1)).endinit(); 
      this.resumelayout(false); 
      this.performlayout(); 
 
    } 
 
    #endregion 
 
    private system.windows.forms.button button1; 
    private system.windows.forms.button button2; 
    private system.windows.forms.picturebox picturebox1; 
    private system.windows.forms.label label1; 
    private system.windows.forms.textbox textbox1; 
    private system.windows.forms.label label2; 
    private system.windows.forms.label lbl解码信息; 
  } 
} 

图形如下所示:

用.NET如何生成二维码

3、编写 编码和解码按钮事件,进行编码和解码处理:

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; 
 
namespace winqrcode 
{ 
  using thoughtworks.qrcode.codec; 
  using thoughtworks.qrcode.codec.data; 
  using thoughtworks.qrcode.codec.util; 
  using system.io; 
  using pdftoimage; 
  public partial class form1 : form 
  { 
    public form1() 
    { 
      initializecomponent(); 
    } 
    /// <summary> 
    /// 编码 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void button1_click(object sender, eventargs e) 
    { 
      if (this.textbox1.text != "") 
      { 
        cursor.current = cursors.waitcursor; 
        if (textbox1.text.trim() == string.empty) 
        { 
          messagebox.show("不能为空."); 
          return; 
        } 
        qrcodeencoder qrcodeencoder = new qrcodeencoder();        
        qrcodeencoder.qrcodeencodemode = qrcodeencoder.encode_mode.byte; 
        qrcodeencoder.qrcodescale = 4; 
        qrcodeencoder.qrcodeversion = 7;       
        qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction.m;        
        system.drawing.image image; 
        string data = textbox1.text; 
        //编码 
        image = qrcodeencoder.encode(data); 
        //图片显示 
        this.picturebox1.image = image; 
        cursor.current = cursors.default; 
 
      } 
    } 
    /// <summary> 
    /// 解码 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void button2_click(object sender, eventargs e) 
    { 
      cursor.current = cursors.waitcursor; 
      bitmap b = this.picturebox1.image as bitmap; 
      try 
      { 
        qrcodedecoder decoder = new qrcodedecoder(); 
        //解码 
        string decodedstring = decoder.decode(new qrcodebitmapimage(b)); 
        //显示解码信息 
        this.lbl解码信息.text += decodedstring ; 
      } 
      catch (exception ex) 
      { 
        messagebox.show(ex.message); 
      } 
      cursor.current = cursors.default; 
    } 
 
    private void form1_load(object sender, eventargs e) 
    { 
 
    } 
  } 
} 

4、编译运行,如下图:

用.NET如何生成二维码

以上就是关于使用.net生成二维码的大概思路,还有许多不足之处,希望大家多提建议,或者自己进行创新修改。

上一篇:

下一篇: