[C #] Two-dimensional code printing

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.drawing.printing;using Thoughtworks.qrcode.codec;using thoughtworks.qrcode.codec.data;using Thoughtworks.qrcode.codec.util;namespace  twodimensioncodeprint{public partial class Print:form {private int left = 0, top = 0, width = max, height        = 150;                private string fileName = "d:\\ qr code e-ticket \ \ Red Valley Grand ticket \\20141025111316 Red River Valley grand entrance ticket total 30. jpg";        Public Print () {InitializeComponent (); }//print private void Btnprint_click (object sender, EventArgs e) {if (pictureBox1.Image = =                NULL) {MessageBox.Show ("Please select a QR code image");            Return            }//Print preview PrintPreviewDialog PrintPreviewDialog = new PrintPreviewDialog ();      PrintDocument PrintDocument = new PrintDocument ();      Set the margin try {left = Int.                Parse (Txtleft.text); top = Int.                Parse (Txttop.text); width = Int.                Parse (Txtwidth.text); height = Int.            Parse (Txtheight.text);            } catch {MessageBox.Show ("Please enter the correct number");            } Margins margin = new Margins (left, left + width, top, top + height);            PrintDocument.DefaultPageSettings.Margins = margin; Print event settings Printdocument.printpage + = new Printpageeventhandler (this.            Printpageevent);            Print Preview//printpreviewdialog.document = PrintDocument;            Printpreviewdialog.showdialog ();            try {printdocument.print (); } catch (Exception ex) {MessageBox.Show (ex.                Message, "Print error", MessageBoxButtons.OK, Messageboxicon.error); PrintDocument.PrintController.OnEndPrint (printdoCument, New PrintEventArgs ()); }}//print event handling private void Printpageevent (object sender, PrintPageEventArgs e) {//Read            Fetch picture//1 Get//Bitmap image = new Bitmap (fileName)            2 obtained from PictureBox//image image = pictureBox1.Image;            3 Print image image = Produceqrcode ("Sigi") directly based on the generated QR code;            Print the QR code in the specified area Rectangle destrect = new Rectangle (left, top, width, height); E.graphics.drawimage (image, Destrect, 0, 0, image. Width, image.        Height, System.Drawing.GraphicsUnit.Pixel);            }//Fills the result into the picture private void getresultintoimage (ref Image Temp, string name, string date) {            Read the picture Graphics g = graphics.fromimage (temp);            Font f = new font ("Arial", 12);            Brush B = new SolidBrush (System.Drawing.Color.Black); Fills the data to the picture g.drawimage (temp, 0, 0, temp. Width, temp.            Height); G.drawString (name, F, B, 168, 105);            g.DrawString (date, F, B, 176, 269);        G.dispose (); }//Generate two-dimensional code public Image Produceqrcode (string ticketcodecontent) {Qrcodeencoder qrcodeencod            ER = new Qrcodeencoder (); Qrcodeencoder.qrcodeencodemode = Qrcodeencoder.encode_mode.            BYTE;            Qrcodeencoder.qrcodescale = 7;            qrcodeencoder.qrcodeversion = 0; Qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction.            L            Qrcodeencoder.qrcodebackgroundcolor = System.Drawing.Color.White;            Qrcodeencoder.qrcodeforegroundcolor = System.Drawing.Color.Black;        Return Qrcodeencoder.encode (ticketcodecontent); }    }}


Operating interface:





Full code Download: Click to open the link



[C #] Two-dimensional code printing

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.