第一步首先:下載下傳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;
}
}
效果圖:
輸入位址後生成的二維碼效果
另存為二維碼