怎么用.NET生成二维码

发布于2022-01-13 02:11:21
4个回答
admin
网友回答2022-01-13
环境:Visual Studio 2013
准备工作:引入一个dll——ThoughtWorks.QRCode
dll作用:封装成熟的二维码算法等(有包不用自己写的,我觉得都是大仙)
引入dll方法:
(1)打开“程序包管理控制台”(视图——其他窗口——程序包管理控制台)
(2)Install-Package -ProjectName 你的项目名称 ThoughtWorks.QRCode
(3)等待等待等待等待。。。。。。。然后装完。

生成二维码的核心代码:
//mapPath:网站根目录//fileSaveFolder:相对路径//link:链接或者文本public static string GenerateLinkQRCode (string mapPath, string fileSaveFolder, string link){ QRCodeEncoder qr = new QRCodeEncoder() { //编码 QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE, //二维码缩放 QRCodeScale = 4, //数字越小,二维码密度越小, //可容纳的文本越少 QRCodeVersion = 4, // QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M }; //存放路径处理 if (!Directory.Exists(mapPath + fileSaveFolder)) { Directory.CreateDirectory(mapPath + fileSaveFolder); } //给二维码图片取名字 //规则可任意修改 string fileName = QRCode + DateTime.Now.ToString(yyyyMMddHHmmss) + 随机字符串 + .png; //存放二维码的绝对路径 string filePath = mapPath + fileSaveFolder + fileName; //生成二维码图片放在内存中 Bitmap bitmap = qr.Encode(link); //保存图片到服务器磁盘(本地保存) Bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Png); //准备返回二维码图片相对路径 string result = fileSaveFolder + fileName; result = / + result.Replace(@\, /); //输出结果,就是二维码的路径 return result;}
admin
网友回答2022-01-13
新建一个window应用程序,然后引入.NET二维码类库(开源的类库,可从网上下载):
核心代码如下:

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)

{

}

}

}
admin
网友回答2022-01-13
errHTML = '''
HTMLHEADTITLE
Friends CGI Demo/TITLE/HEAD
BODYH3ERROR/H3
B%s/BP
FORMINPUT TYPE=button VALUE=Back
'''
cursor.execute('''
CREATE TABLE users (
login VARCHAR(8),
uid INTEGER,
prid INTEGER)
''')
admin
网友回答2022-01-13
检查一下目录是否有写入的权限

回到
顶部