Use QrCodeNet in MVC to generate a QR code and mvcqrcodenet to generate a QR code.

Source: Internet
Author: User

Use QrCodeNet in MVC to generate a QR code and mvcqrcodenet to generate a QR code.

QrCodeNet: http://qrcodenet.codeplex.com/

1 using System. drawing; 2 using System. drawing. imaging; 3 using System. IO; 4 using Gma. qrCodeNet. encoding; 5 using Gma. qrCodeNet. encoding. windows. render; 6 7 namespace QRCodeTest 8 {9 public class QRCodeHelper10 {11 /// <summary> 12 // generate QR code 13 /// </summary> 14 /// <param name = "content"> content </param> 15 /// <param name = "moduleSize"> QR code size </param> 16 /// <returns> output stream </returns> 17 public static MemoryStream GetQRCode (string content, int moduleSize = 9) 18 {19 // ErrorCorrectionLevel Error Correction level 20 // QuietZoneModules blank area 21 22 var encoder = new QrEncoder (ErrorCorrectionLevel. m); 23 QrCode qrCode = encoder. encode (content); 24 GraphicsRenderer render = new GraphicsRenderer (new FixedModuleSize (moduleSize, QuietZoneModules. two), Brushes. black, Brushes. white); 25 26 MemoryStream memoryStream = new MemoryStream (); 27 render. writeToStream (qrCode. matrix, ImageFormat. jpeg, memoryStream); 28 29 return memoryStream; 30 31 // code for generating images 32 // DrawingSize dSize = render. sizeCalculator. getSize (qrCode. matrix. width); 33 // Bitmap map = new Bitmap (dSize. codeWidth, dSize. codeWidth); 34 // Graphics g = Graphics. fromImage (map); 35 // render. draw (g, qrCode. matrix); 36 // map. save (fileName, ImageFormat. jpeg ); // fileName is the path of the stored image 37} 38 39 // <summary> 40 // generate a QR code 41 /// </summary> 42 // <param name = "content"> content </param> 43 // <param name = "iconPath"> logo path </param> 44 // <param name = "moduleSize"> QR code size </param> 45 // <returns> output stream </returns> 46 public static MemoryStream GetQRCode (string content, string iconPath, int moduleSize = 9) 47 {48 QrEncoder qrEncoder = new QrEncoder (ErrorCorrectionLevel. m); 49 QrCode qrCode = qrEncoder. encode (content); 50 51 GraphicsRenderer render = new GraphicsRenderer (new FixedModuleSize (moduleSize, QuietZoneModules. two), Brushes. black, Brushes. white); 52 53 DrawingSize dSize = render. sizeCalculator. getSize (qrCode. matrix. width); 54 Bitmap map = new Bitmap (dSize. codeWidth, dSize. codeWidth); 55 Graphics g = Graphics. fromImage (map); 56 render. draw (g, qrCode. matrix); 57 58 // append the Logo image. Pay attention to controlling the ratio of the Logo image size to the QR code. 59 // PS: If the append image is too large, the information will be lost if the ratio of the two-dimensional code exceeds the limit, cannot be recognized 60 Image img = Image. fromFile (iconPath); 61 62 Point imgPoint = new Point (map. width-img. width)/2, (map. height-img. height)/2); 63g. drawImage (img, imgPoint. x, imgPoint. y, img. width, img. height); 64 65 MemoryStream memoryStream = new MemoryStream (); 66 map. save (memoryStream, ImageFormat. jpeg); 67 68 return memoryStream; 69 70 // The code for generating the image: map. save (fileName, ImageFormat. jpeg); // fileName indicates the path of the stored image 71} 72} 73}

 

Controller:

 1 public ActionResult CreateQrCode() 2  { 3        string str = "http://cn.bing.com/search?q=C%23"; 4        using (var memoryStream = QRCodeHelper.GetQRCode(str, Server.MapPath(@"~/images/logo.jpg"))) 5        { 6              Response.ContentType = "image/jpeg"; 7              Response.OutputStream.Write(memoryStream.GetBuffer(), 0, (int)ms.Length); 8              Response.End(); 9        }10        return null;11 }

View:

1 

The effect is as follows:

Related Article

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.