Winfrom writing QR code (can upload images)

Source: Internet
Author: User

Write a personal information QR code:

First download a ThoughtWorks.QRCode.dll that generates a QR code (note: Add a reference to the project after downloading it)

Then design a form:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using ThoughtWorks.QRCode.Codec;

Namespace Two_dimensioncode
{
public partial class Myselfinfo:form
{
Public Myselfinfo ()
{
InitializeComponent ();
}

<summary>
/Get QR Code
</summary>
<param name= "Write" ></param>
<returns></returns>
Public Bitmap GetCode (string write)
{
StringBuilder builder = new StringBuilder ();
Builder. Append ("Name:" + this.txtname);
Qrcodeencoder code = new Qrcodeencoder ();
Code. Qrcodeencodemode = Qrcodeencoder.encode_mode. BYTE;
Code. Qrcodescale = 4;
Code. Qrcodeversion = 8;
Code. Qrcodeerrorcorrect = qrcodeencoder.error_correction. M
Bitmap image = code. Encode (write);
return image;
}

<summary>
Get logo
</summary>
<returns></returns>
Public Bitmap Getlogo ()
{
Bitmap newbmp = new Bitmap ("logo.jpg");//Get Picture Object
Bitmap bmp = New Bitmap (newbmp, 30, 30);//Zoom
return BMP;
}

Private object logoimg;//for storing logo images

<summary>
Generated
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Btngenerate_click (object sender, EventArgs e)
{
if (This.txtName.Text.Trim () = "")//Verify that the generated content entered is empty
{
Bitmap Bcode = GetCode (This.txtName.Text);//Get two-dimensional code picture
if (logoimg = = null)
{
Pbcode.image = Bcode;
}
Else
{
Bitmap Blogo = logoimg as bitmap;//get logo image Object
Blogo = new Bitmap (Blogo, 30, 30);//change the size of the picture here we set to 30
int Y = Bcode.height;
int X = Bcode.width;
Point, X/2, Y/2,//logo the image is drawn to the QR code, where the coordinates of the logo are simply calculated.
Graphics g = graphics.fromimage (Bcode);//Create a canvas
G.drawimage (Blogo, point);//Map the logo image to a QR code image
Pbcode.image = Bcode;
}
}
Else
{
MessageBox.Show ("Input generated content! ");
}
}

<summary>
Save
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Btnsave_click (object sender, EventArgs e)
{
SaveFileDialog SaveFile = new SaveFileDialog ();//Create Save dialog box
Savefile.filter = "Jpeg|*.jpeg;*.jpg| bitmap file |*.bmp| all Files |*.*";//Set the saved picture format
if (pbcode.image! = null)
{
if (savefile.showdialog () = = DialogResult.OK)
{
string sfilepathname = Savefile.filename;
Image img = pbcode.image;
Img. Save (Sfilepathname);
}
}
Else
{
MessageBox.Show ("Please Sir into two-dimensional code!") ");
}
}

<summary>
Upload logo
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Btnlogo_click (object sender, EventArgs e)
{
OpenFileDialog OpenFileDialog = new OpenFileDialog ();//File Open dialog box
Openfiledialog.filter = "Jpeg|*.jpeg;*.jpg| bitmap file |*.bmp| all Files |*.*";//Only open these types of files that we set
if (openfiledialog.showdialog () = = DialogResult.OK)
{
string fileName = Openfiledialog.filename;
Bitmap newbmp = new Bitmap (fileName);
Bitmap bmp = new Bitmap (newbmp);
Pbcode.image = BMP;
logoimg = BMP;
}
}
}
}

Next, create a QRCodeBuilder.cs class:

The code is as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Drawing;
Using System.IO;
Using System.Windows.Forms;
Using ThoughtWorks.QRCode.Codec;

Namespace Tianqrcode
{
public class Qrcodebuilder
{
<summary>
Create two-dimensional code images
</summary>
<param name= "Codestr" > string to generate a QR code </param>
<returns> returns the generated image</returns>
Public Image Createqrcode (string codestr)
{
Qrcodeencoder encoder = new Qrcodeencoder ();
Encoder. Qrcodeencodemode = Qrcodeencoder.encode_mode. byte;//Encoding method
Encoder. Qrcodescale = 4;//Size
Encoder. Qrcodeversion = 8;//Version
Encoder. Qrcodeerrorcorrect = qrcodeencoder.error_correction. M
System.Drawing.Bitmap BP = encoder. Encode (Codestr, encoding.getencoding ("GB2312"));
Image image = BP;

Save two-dimensional code image:
SaveFileDialog SF = new SaveFileDialog ();
sf. Title = "Choose Save File Location";
sf. Filter = "Save Picture (*.jpg) |*.jpg| All Files (*. *) |*.*";
Set default file type display order
sf. FilterIndex = 1;
Save dialog box remembers the last Open Directory
sf. Restoredirectory = true;
if (SF. ShowDialog () = = DialogResult.OK)
{
Get file path
String Localfilepath = SF. Filename.tostring ();
Image. Save (Localfilepath);
}
return image;
}
}
}

Winfrom writing QR code (can upload images)

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.