ASP. NET (C #) uses QRCode to generate a QR code (cont.)-Add a logo or image to the image center of the QR code.

Source: Internet
Author: User

  1. <%@ WebHandler language="C #" class="Getqrcode"%>
  2. Using System;
  3. Using System.Web;
  4. Using ThoughtWorks.QRCode.Codec;
  5. Using ThoughtWorks.QRCode.Codec.Data;
  6. Using ThoughtWorks.QRCode.Codec.Util;
  7. Using System.IO;
  8. Using System.Text;
  9. Using System.Drawing;
  10. Using System.Drawing.Drawing2D;
  11. Public class Getqrcode:ihttphandler
  12. {
  13. public void ProcessRequest (HttpContext context)
  14. {
  15. String data = context.  request["Codetext"];
  16. if (! String. IsNullOrEmpty (data))
  17. {
  18. Qrcodeencoder Qrcodeencoder = new Qrcodeencoder ();
  19. Qrcodeencoder.qrcodeencodemode = Qrcodeencoder.encode_mode. BYTE;
  20. Qrcodeencoder.qrcodescale = 4;
  21. Qrcodeencoder.qrcodeversion = 8;
  22. Qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction. M
  23. System.Drawing.Image Image = qrcodeencoder.encode (data);
  24. System.IO.MemoryStream Mstream = new System.IO.MemoryStream ();
  25. Image. Save (Mstream, System.Drawing.Imaging.ImageFormat.Png);
  26. System.IO.MemoryStream MStream1 = new System.IO.MemoryStream ();
  27. Combinimage (image, context. Server.MapPath ("~/images/201292891051540.jpg")).  Save (MStream1, System.Drawing.Imaging.ImageFormat.Png);
  28. Context. Response.clearcontent ();
  29. Context.  Response.ContentType = "Image/png";
  30. Context. Response.BinaryWrite (Mstream1.toarray ());
  31. //image.   Dispose ();
  32. Mstream.dispose ();
  33. Mstream1.dispose ();
  34. }
  35. Context. Response.Flush ();
  36. Context. Response.End ();
  37. }
  38. // <summary>
  39. //// Call this function to merge the two pictures, like the album, there is a
  40. /// background map, paste your own target image in the middle
  41. // </summary>
  42. /// <param name= "Imgback" > Paste the source picture </param>
  43. /// <param name= "destimg" > pasted target picture </param>
  44. public static image combinimage (image imgback, string destimg)
  45. {
  46. Image img = image.fromfile (destimg); //Photo gallery
  47. if (img. Height! = 65 | | Img. Width! =)
  48. {
  49. img = Kiresizeimage (IMG, 65, 65, 0);
  50. }
  51. Graphics g = graphics.fromimage (Imgback);
  52. G.drawimage (imgback, 0, 0, imgback.width, imgback.height);      //g.drawimage (imgback, 0, 0, frame width, frame height);
  53. //g.fillrectangle (System.Drawing.Brushes.White, IMGBACK.WIDTH/2-img. WIDTH/2-1, IMGBACK.WIDTH/2-img. WIDTH/2-1,1,1);//paint a black border around the photo
  54. //g.drawimage (img, photo with picture frame on the left margin, photo with frame top margin, photo width, photo high);
  55. G.drawimage (IMG, IMGBACK.WIDTH/2-img. WIDTH/2, Imgback.width/2-img. WIDTH/2, IMG. Width, IMG. Height);
  56. Gc. Collect ();
  57. return imgback;
  58. }
  59. // <summary>
  60. /// Resize Pictures
  61. // </summary>
  62. /// <param name= "BMP" > Original bitmap</param>
  63. /// <param name= "NEWW" > New width </param>
  64. /// <param name= "NEWH" > New heights </param>
  65. /// <param name= "Mode" > reserved, temporarily unused </param>
  66. /// <returns> pictures after processing </returns>
  67. public static image kiresizeimage (image bmp, int neww, int newh, int Mode)
  68. {
  69. Try
  70. {
  71. Image B = new Bitmap (NEWW, NEWH);
  72. Graphics g = graphics.fromimage (b);
  73. the quality of the//interpolation algorithm
  74. G.interpolationmode = Interpolationmode.highqualitybicubic;
  75. G.drawimage (BMP, new Rectangle (0, 0, NEWW, NEWH), new Rectangle (0, 0, BMP. Width, BMP.  Height), GraphicsUnit.Pixel);
  76. G.dispose ();
  77. return B;
  78. }
  79. Catch
  80. {
  81. return null;
  82. }
  83. }
  84. public bool IsReusable
  85. {
  86. Get
  87. {
  88. return false;
  89. }
  90. }
  91. }

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.