C # generates two-dimensional code (with decoding)

Source: Internet
Author: User

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 system.collections;using com.google.zxing;//    Need to download using system.text.regularexpressions;using Bytematrix = com.google.zxing.common.bytematrix;namespace qr code from the Web        Public partial class Form1:form {public Form1 () {InitializeComponent (); }//Generate barcode private void Button1_Click (object sender, EventArgs e) {regex RG = new Regex ("^ [0-9]            {13}$ "); if (!RG.                IsMatch (TextBox1.Text)) {MessageBox.Show ("This example uses EAN_13 encoding and requires 13 digits to be entered");            Return                } try {Multiformatwriter mutiwriter = new Com.google.zxing.MultiFormatWriter ();                Bytematrix BM = Mutiwriter.encode (TextBox1.Text, com.google.zxing.BarcodeFormat.EAN_13, 363, 150); Bitmap img = BM.                Tobitmap ();                pictureBox1.Image = img; Automatically save picture to current directory string filename = System.Environment.CurrentDirectory + "\\EAN_13" + DateTime.Now.Ticks.ToStr                ING () + ". jpg"; Img.                Save (filename, System.Drawing.Imaging.ImageFormat.Jpeg); Label2.            Text = "Picture saved to:" + filename; } catch (Exception ee) {MessageBox.Show (EE.            Message);            }}//Generate two-dimensional code private void button2_click (object sender, EventArgs e) {try                {Multiformatwriter Mutiwriter = new Com.google.zxing.MultiFormatWriter ();                Bytematrix BM = Mutiwriter.encode (TextBox1.Text, Com.google.zxing.BarcodeFormat.QR_CODE, 300, 300); Bitmap img = BM.                Tobitmap ();                pictureBox1.Image = img;                string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString () + ". jpg"; Img. SavE (filename, System.Drawing.Imaging.ImageFormat.Jpeg); Label2.            Text = "Picture saved to:" + filename; } catch (Exception ee) {MessageBox.Show (EE.             Message);            }}//Generate two-dimensional code with picture private void Button3_Click (object sender, EventArgs e) {try                {Multiformatwriter Mutiwriter = new Com.google.zxing.MultiFormatWriter ();                Hashtable hint = new Hashtable (); Hint.                ADD (Encodehinttype.character_set, "UTF-8"); Hint.                ADD (Encodehinttype.error_correction, com.google.zxing.qrcode.decoder.errorcorrectionlevel.h);                Bytematrix BM = Mutiwriter.encode (TextBox1.Text, Com.google.zxing.BarcodeFormat.QR_CODE, +, hint); Bitmap img = BM.                Tobitmap ();                Image middlimg = qrmiddleimg.image; System.Drawing.Size realsize = mutiwriter.getencodesize (TextBox1.Text, Com.google.zxing.BarcodeFormat.QR_CODE, 300, 300);                Calculate the size and position of the inserted picture int MIDDLEIMGW = math.min ((int) (realsize.width/3.5), middlimg.width);                int middleimgh = math.min ((int) (realsize.height/3.5), middlimg.height); int MIDDLEIMGL = (img.                WIDTH-MIDDLEIMGW)/2; int MIDDLEIMGT = (img.                HEIGHT-MIDDLEIMGH)/2; The IMG is converted to BMP format, otherwise the graphics object cannot be created later Bitmap bmpimg = new Bitmap (img. Width, IMG.                Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics g = graphics.fromimage (bmpimg)) {G.interpolationmode = SYSTEM.DRAWING.D                    Rawing2D.InterpolationMode.HighQualityBicubic;                    G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;                    g.compositingquality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;                G.drawimage (IMG, 0, 0); }//Insert picture in QR code System.drawIng.                Graphics mygraphic = System.Drawing.Graphics.FromImage (bmpimg);                White-bottomed Mygraphic.fillrectangle (Brushes.white, MIDDLEIMGL, Middleimgt, MIDDLEIMGW, MIDDLEIMGH);                Mygraphic.drawimage (middlimg, MIDDLEIMGL, Middleimgt, MIDDLEIMGW, MIDDLEIMGH);                pictureBox1.Image = bmpimg; Automatically save the picture to the current directory string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString (                ) + ". jpg"; Bmpimg.                Save (filename, System.Drawing.Imaging.ImageFormat.Jpeg); Label2.            Text = "Picture saved to:" + filename; } catch (Exception ee) {MessageBox.Show (EE.             Message);        }} String opfilepath = ""; Select the intermediate picture to generate private void Picturebox2_click (object sender, EventArgs e) {Openfiledialog1.filter            = "Picture file |*.bmp;*.jpg;*.png;*.ico";          if (openfiledialog1.showdialog () = = DialogResult.OK)  {Opfilepath = Openfiledialog1.filename;                Image img = image.fromfile (opfilepath);            Qrmiddleimg.image = img; }}//Select the picture to decode private void Button4_Click (object sender, EventArgs e) {Openfiledia Log1.            Filter = "picture file |*.bmp;*.jpg;*.png;*.ico";                if (openfiledialog1.showdialog () = = DialogResult.OK) {Opfilepath = Openfiledialog1.filename;            Picturebox2.imagelocation = Opfilepath; }}//decoding the QR code private void Button5_click (object sender, EventArgs e) {Multiformatrea            Der Mutireader = new Com.google.zxing.MultiFormatReader ();            Bitmap img = (Bitmap) bitmap.fromfile (Opfilepath);            if (img = = null) return; Luminancesource ls = new Rgbluminancesource (IMG, IMG. Width, IMG.            Height); Binarybitmap BB = new Binarybitmap (new Com.google.zxing.common.HybridBinarizer (LS));            Note must be UTF-8 encoded Hashtable hints = new Hashtable (); Hints.            ADD (Encodehinttype.character_set, "UTF-8");            Result r = Mutireader.decode (BB, hints);        richTextBox1.Text = R.text; }    }}

C # generates two-dimensional code (with decoding)

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.