您的位置:首页 > 其它

Winform 利用ThoughtWorks.QRCode 生成二维码,并另存为二维码图片(限制大小)

2016-05-11 09:23 513 查看
第一步首先:下载ThoughtWorks.QRCode.dll 放置到项目Bin目录下,然后右键项目添加引用;

第二步界面代码:

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.groupBox1.Location = new System.Drawing.Point(0, 13);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(462, 84);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "产品地址";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(6, 23);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(429, 44);
this.textBox1.TabIndex = 0;
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(13, 225);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(89, 38);
this.button1.TabIndex = 2;
this.button1.Text = "生成二维码";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button2.Location = new System.Drawing.Point(127, 225);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(89, 37);
this.button2.TabIndex = 3;
this.button2.Text = "下载二维码";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(13, 118);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(77, 14);
this.label1.TabIndex = 4;
this.label1.Text = "产品名称:";
//
// textBox2
//
this.textBox2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.textBox2.Location = new System.Drawing.Point(16, 149);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(220, 43);
this.textBox2.TabIndex = 5;
//
// pictureBox1
//
this.pictureBox1.Image = global::QRBarCode.Properties.Resources.a3fe94fc_f4e0_4f23_b3da_baeab3412ab01;
this.pictureBox1.Location = new System.Drawing.Point(261, 118);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(156, 145);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 6;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(444, 306);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox1);
this.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.Name = "Form1";
this.Text = "库买国际二维码图片生成";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.PictureBox pictureBox1;

第三步后台代码:

using ThoughtWorks.QRCode.Codec; 引用

private void button1_Click(object sender, EventArgs e)
{
ThoughtWorks.QRCode.Codec.QRCodeEncoder barcode = new QRCodeEncoder();
barcode.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字)
barcode.QRCodeScale = 4;//大小(值越大生成的二维码图片像素越高)
barcode.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误)
try
{
barcode.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//错误效验、错误更正(有4个等级)
String qrdata = textBox1.Text.Trim();
System.Drawing.Bitmap bp = barcode.Encode(qrdata.ToString(), Encoding.GetEncoding("GB2312"));
// Image image = bp;
pictureBox1.Image = bp;
}
catch //这个异常处理是放在输入内容太多造成数组溢出等异常
{
MessageBox.Show("生成失败,请点击重新生成.");
}

}

private void button2_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox2.Text))
{
SaveFileDialog sf = new SaveFileDialog();
sf.Title = "选择保存文件位置";
sf.Filter = "保存图片(*.jpg) |*.jpg|所有文件(*.*) |*.*";
//设置默认文件类型显示顺序
sf.FilterIndex = 1;
//设置文件名称:
sf.FileName = textBox2.Text + ".jpg";

//保存对话框是否记忆上次打开的目录
sf.RestoreDirectory = true;

if (sf.ShowDialog() == DialogResult.OK)
{
//获得文件路径
string localFilePath = sf.FileName.ToString();
if (sf.FileName != "")
{
string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//获取文件名,不带路径
string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf(".")); //获取文件路径,带文件名,不带后缀
//string fn = sf.FileName;
Image img = new Bitmap(this.pictureBox1.Image, 60, 60);//另存为图片大小限制
img.Save(FilePath + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
MessageBox.Show("保存成功");
}
}
}
else
{
MessageBox.Show("请输入产品名称"); return;
}
}

效果图:



输入地址后生成的二维码效果



另存为二维码

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息