C# WinForm创建QR Code测试

完整效果如下图:

实现的功能:
1. 创建QR Barcode

2. 把QR Barcode存储到文件

3. 读取存储的QR Code文件内容

4. 创建vCard 个人电子名片

首先添加Zxing 的引用

后台添加引用

using ZXing;

using ZXing.QrCode;

1. 创建QR Barcode

var options = new QrCodeEncodingOptions

{

Width = this.pictureBox1.Width,

Height = this.pictureBox1.Height

};

var writer = new BarcodeWriter();

writer.Format = BarcodeFormat.QR_CODE;

writer.Options = options;

var barCode = writer.Write(txtData.Text);

pictureBox1.Image = barCode;

将输入的文本信息创建一个QR Code的 Bitmap对象,将对象赋值给Picture控件的Image属性。

2. 存储QR Code到文件

存储文件效果如下:

实现代码如下:

string initDir = @“C:\Users\Desktop\QRCode\images”;

var sfd = new SaveFileDialog();

sfd.InitialDirectory = initDir;

sfd.Filter = “Image Files (*.bmp, *.png, *.jpg)|*.bmp;*.png;*.jpg | All Files | *.*”;

if(sfd.ShowDialog()== DialogResult.OK)

{

pictureBox1.Image.Save(sfd.FileName);

}

3. 读取存储的QR Barcode文件内容

var ofd = new OpenFileDialog();

if (ofd.ShowDialog() == DialogResult.OK)

{

var reader = new BarcodeReader();

var imagefile = Image.FromFile(ofd.FileName) as Bitmap;

pictureBox1.Image = imagefile;

var result = reader.Decode(imagefile);

txtData.Text = result.Text;

}

选择存储的QR 图片文件,读取文件内容,显示条码到UI Picture中。

4. 创建创建vCard 个人电子名片

var options = new QrCodeEncodingOptions

{

Width = this.pictureBox1.Width,

Height = this.pictureBox1.Height

};

var writer = new BarcodeWriter();

writer.Format = BarcodeFormat.QR_CODE;

writer.Options = options;

// 固定文本,仅供参参考,可以修其中的名字,地址电话号码等等

var text = $“BEGIN:VCARD\n” +

$“VERSION: 3.0\n” +

$“N: Krabs; Eugene; ; mr;\n” +

$“FN:Mr Eugene Krabs\n” +

$“TITLE:Owner\n” +

$“ORG:Krusty Krab\n” +

$“URL; WORK: http://www.krustykrab.com\n” +

$“TEL; TYPE = WORK:+1 - 222 - 222 - 2222\n” +

$“TEL; TYPE = CELL,VOICE: +1 - 333 - 333 - 3333\n” +

$“TEL; TYPE = FAX:+1 - 444 - 444 - 4444\n” +

$“EMAIL; INTERNET: eugene @kk.com\n” +

$“ADR; INTL; PARCEL; WORK; CHARSET = utf - 8:;; One Krabby Patty Dr; BikiniBottom; Hi;03122, USA\n” +

$“NOTE; CHARSET = utf - 8:best krabby patty in town\n” +

$“END:VCARD”;

var barCode = writer.Write(text);

pictureBox1.Image = barCode;

创建的QR Code,用微信扫一扫,效果如下:

一个完整的电子名片完成,可以将姓名,电话,地址,等等信息修改为自己的实际值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysh05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值