C # instance code written by zxing QR code

Source: Internet
Author: User

Copy codeThe Code is as follows: private void button#click (object sender, EventArgs e)
{
If (string. IsNullOrEmpty (this. textBox1.Text. Trim ()))
{
MessageBox. Show ("enter the information to be converted! ");
Return;
}

String content = textBox1.Text;

Hashtable hints = new Hashtable ();
Hints. Add (EncodeHintType. ERROR_CORRECTION, ErrorCorrectionLevel. L); // Error Correction level
Hints. Add (EncodeHintType. CHARACTER_SET, "UTF-8"); // encoding format

ByteMatrix byteMatrix = new MultiFormatWriter (). encode (content, BarcodeFormat. QR_CODE, 300,300, hints );
Bitmap bitmap = toBitmap (byteMatrix );
PictureBox1.Image = bitmap;

SaveFileDialog sFD = new SaveFileDialog ();
SFD. Filter = "*. png | *. png ";
SFD. AddExtension = true;
Try
{
If (sFD. ShowDialog () = DialogResult. OK)
{
WriteToFile (byteMatrix, System. Drawing. Imaging. ImageFormat. Png, sFD. FileName );
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}

}

Public static void writeToFile (ByteMatrix, System. Drawing. Imaging. ImageFormat format, string file)
{
System. Drawing. Imaging. EncoderParameters eps = new System. Drawing. Imaging. EncoderParameters ();
Eps. Param [0] = new System. Drawing. Imaging. EncoderParameter (System. Drawing. Imaging. Encoder. Quality, 100L );
Bitmap bmap = toBitmap (matrix );
Bmap. Save (file, format );
}
Public static Bitmap toBitmap (ByteMatrix)
{
Int width = matrix. Width;
Int height = matrix. Height;
Bitmap bmap = new Bitmap (width, height, System. Drawing. Imaging. PixelFormat. Format32bppArgb );
For (int x = 0; x <width; x ++)
{
For (int y = 0; y {
Bmap. SetPixel (x, y, matrix. get_Renamed (x, y )! =-1? ColorTranslator. FromHtml ("Purple"): ColorTranslator. FromHtml ("0 xFFFFFFFF"); // you can customize the color and background color.
}
}

Return bmap;
}

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.