C#创建二维码(利用QRcode)

更新时间:2023-05-25 23:10:53 阅读: 评论:0

关于用c#创建二维码的代码
直接上代码了,主要是解决形成中文二维码
如果需要下载源码,可以百度搜素:C#创建二维码利用QRcode cnapsoft
或者进入/download/cnapsoft/4755855
Winform程序(在界面上创建的控件)
privateSystem.Windows.Forms.Buttonbutton1;
privateSystem.Windows.Forms.PageSetupDialogpageSetupDialog1;
privateSystem.Windows.Forms.ComboBoxcboCorrectionLevel;
privateSystem.Windows.Forms.TextBoxtxtSize;
privateSystem.Windows.Forms.ComboBoxcboVersion;
privateSystem.Windows.Forms.ComboBoxcboEncoding;
privateSystem.Windows.Forms.Labellabel1;
永恒的爱英文
privateSystem.Windows.Forms.Labellabel6;
privateSystem.Windows.Forms.Labellabel7;
privateSystem.Windows.Forms.Labellabel8;
privateSystem.Windows.Forms.TextBoxtxtEncodeData;
privateSystem.Windows.Forms.GroupBoxgroupBox1;
privateSystem.Windows.Forms.GroupBoxgroupBox2;
privateSystem神鬼运转.Windows.Forms.PictureBoxpb_view;四级证书编号查询
privateSystem.Windows.Forms.Buttonbt_save;
privateSystem.Windows.Forms.ButtonbtnOpen;
privateSystem.Windows.Forms.ButtonbtnDecode;
引用类库:
usingThoughtWorks.QRCode.Codec;
usingThoughtWorks.QRCode.Codec.Data;
usingThoughtWorks.QRCode.Codec.Util;
以下为源代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingThoughtWorks.QRCode.Codec;
usingThoughtWorks.QRCode.Codec.Data;
usingThoughtWorks.QRCode.Codec.Util;
namespacetest
{
publicpartialclassQRCode : Form
    {
publicQRCode()
        {
InitializeComponent();
        }
privatevoidbutton1_Click(objectnder, EventArgse)
        {
if (txtEncodeData.Text.Trim() == String.Empty)
            {
MessageBox.Show("数据不能为空");
return;
            }
QRCodeEncoderqrCodeEncoder = newQRCodeEncoder();
Stringencoding = cboEncoding.Text;
if (encoding == "Byte")
            {
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            }
elif (encoding == "AlphaNumeric")
            {
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
            }
elif (encoding == "Numeric")
            {
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
            }
try
            {
intscale = Convert.ToInt16(txtSize.Text);
目睹的意思qrCodeEncoder.QRCodeScale = scale;
            }
catch (Exceptionex)
            {
MessageBox.Show("无效的大小" + ex.Message.ToString());
return;
            }
try
            {
intversion = Convert.ToInt16(cboVersion.Text);
trueloveqrCodeEncoder.QRCodeVersion = version;
            }
catch (Exceptionex)
            {
出国留学迈格森MessageBox.godyShow("无效的版本" + ex.Message.ToString());
            }
stringerrorCorrect = cboCorrectionLevel.Text;
if (errorCorrect == "L")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
elif (errorCorrect == "M")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
elif (errorCorrect == "Q")
qrCodeEncoder.QRCodeErrorCorrect = 新视野大学英语读写教程4QRCodeEncoder.ERROR_CORRECTION.Q;
elif (errorCorrect == "H")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
Imageimage;
Stringdata = txtEncodeData.Text;
try
            {
image = qrCodeEncoder.Encode(data, Encoding.Default);
safari            }
catch (Exceptione1)
            {
MessageBox.Show(e1.ToString());
return;
            }
pb_view.Image = image;
        }
privatevoidQRCode_Load(objectnder, EventArgse)
        {
cboEncoding.SelectedIndex = 2;
cboVersion.SelectedIndex = 6;颜色看性格
cboCorrectionLevel.SelectedIndex = 1;
        }
privatevoidbt_save_Click(objectnder, EventArgse)
        {
SaveFileDialogsaveFileDialog1 = newSaveFileDialog();
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PNG Image|*.png";
saveFileDialog1.Title = "--Save--";
saveFileDialog1.FileName = string.Empty;
//saveFileDialog1.ShowDialog();
if (saveFileDialog1.ShowDialog() != DialogResult.OK)
return;
if (saveFileDialog1.FileName == null)
return;
// save to file
pb_view.Image.Save(saveFileDialog1.FileName);
        }
privatevoidbtnDecode_Click(objectnder, EventArgse)
        {
QRCodeDecoderdecoder = newQRCodeDecoder();
StringdecodedString = decoder.decode(newQRCodeBitmapImage(newBitmap(pb_view.Image)),Encoding.Default);
txtEncodeData.Text = decodedString;
        }
privatevoidbtnOpen_Click(objectnder, EventArgse)
        {
OpenFileDialogopenFileDialog1= newOpenFileDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)

本文发布于:2023-05-25 23:10:53,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/122656.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:创建   二维码   界面   读写   需要
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图