Winform using Thoughtworks.qrcode to generate two-dimensional code, and save as a two-dimensional code image (limit size)

Source: Internet
Author: User

The first step: Download ThoughtWorks.QRCode.dll to the project Bin directory, and then right-click the project to add references;

Second Step interface code:

  #region the code generated by the Windows Forms Designer///<summary>///designer supports the required method-do not use the Code Editor to modify the contents of this method. </summary> private void InitializeComponent () {this.groupbox1 = new System.Windows.For Ms.            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 ("Arial", 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 = "Product Address";            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 ("Arial", 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 = "Generate two-dimensional code";            This.button1.UseVisualStyleBackColor = true;            This.button1.Click + = new System.EventHandler (This.button1_click); Button2//This.button2.Font = new System.Drawing.Font ("Arial", 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 = "Download QR code";            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 ("Arial", 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 = "Product Name:"; TextBox2//This.textBox2.Font = new System.Drawing.Font ("Arial", 12F, System.drawin            G.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.a3            FE94FC_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 ("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GrAphicsunit.point, ((byte) (134))); This.            Name = "Form1"; This.            Text = "Library Buy international two-dimensional code image generation"; 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;


The third step backstage code:

Using ThoughtWorks.QRCode.Codec; Reference

  private void Button1_Click (object sender, EventArgs e) {ThoughtWorks.QRCode.Codec.QRCodeEncoder Barco            de = new Qrcodeencoder (); Barcode. Qrcodeencodemode = Qrcodeencoder.encode_mode. byte;//encoding Method (Note: Byte can support Chinese, alpha_numeric scan out is the number) barcode. Qrcodescale = 4;//size (the larger the value is, the higher the image pixel of the two-dimensional code generated) barcode. Qrcodeversion = 0;//Version (note: setting to 0 is primarily to prevent an error when the encoded string is too long) try {barcode. Qrcodeerrorcorrect = qrcodeencoder.error_correction.                m;//error validation, error correction (with 4 ratings) String Qrdata = TextBox1.Text.Trim (); System.Drawing.Bitmap BP = barcode. Encode (Qrdata.               ToString (), encoding.getencoding ("GB2312"));                Image image = BP;            pictureBox1.Image = BP;            } catch//This exception handler is placed in the input content too many causes an array overflow exception {MessageBox.Show ("Build failed, click Regenerate."); }} private void Button2_Click (object sender, EventArgs e) {if (!string. IsnullorEmpty (TextBox2.Text)) {SaveFileDialog SF = new SaveFileDialog (); sf.                Title = "Choose Save File Location"; sf.                Filter = "Save Picture (*.jpg) |*.jpg| All Files (*. *) |*.*"; Sets the default file type display order SF.                FilterIndex = 1; Set file name: SF.                FileName = TextBox2.Text + ". jpg"; Saves the dialog box to remember the last Open Directory SF.                Restoredirectory = true; if (SF. ShowDialog () = = DialogResult.OK) {//Get file path string localfilepath = SF .                    Filename.tostring (); if (SF. FileName = "") {string filenameext = localfilepath.substring (localfilepath.las Tindexof ("\ \") + 1);//Gets the file name without the path string FilePath = localfilepath.substring (0, Localfilepath.lastind  Exof (".")); Gets the file path, with file name, with no suffix//string fn = sf.                        FileName; Image img = new Bitmap (this.pictureBox1.Image, 60, 60);//Save as Picture size limit img.                        Save (FilePath + ". jpg", System.Drawing.Imaging.ImageFormat.Jpeg);                     MessageBox.Show ("saved successfully");            }}} else {MessageBox.Show ("Please enter product name"); return; }        }


Two-dimensional code effect generated after entering address

Save as two-dimensional code


Winform using Thoughtworks.qrcode to generate two-dimensional code, and save as a two-dimensional code image (limit size)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.