QRCodeHelper QR code generation, qrcodehelper

Source: Internet
Author: User

QRCodeHelper QR code generation, qrcodehelper

QRCodeHelper QR code generation

1 using System; 2 using System. drawing; 3 using ThoughtWorks. QRCode. codec; 4 using System. IO; 5 using System. text; 6 using System. drawing. drawing2D; 7 using System. web; 8 9 namespace APP. qrCodes 10 {11 public class QRCodeHelper 12 {13 public enum ImgEnum 14 {15 JPG 16} 17 18 public static string GetImgType (ImgEnum jpg) 19 {20 string ext = string. empty; 21 switch (jpg) 22 {23 case ImgEnum. JPG: 24 ext = ". jpg "; 25 break; 26 default: 27 break; 28} 29 return ext; 30} 31 // <summary> 32 // generate a QR code image 33 // </summary> 34 // <param name = "codeNumber"> string </param> 35 /// <param name = "size"> size </param> 36 /// <returns> QR code image </returns> 37 public Bitmap Create_ImgCode (string codeNumber, int size) 38 {39 // create a QR code generation class 40 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder (); 41 // set the encoding mode 42 qrCo DeEncoder. QRCodeEncodeMode = QRCodeEncoder. ENCODE_MODE.BYTE; 43 // sets the encoding measurement level 44 qrCodeEncoder. QRCodeScale = size; 45 // set the encoding version 46 qrCodeEncoder. QRCodeVersion = 0; 47 // set the Encoding Error to correct 48 qrCodeEncoder. QRCodeErrorCorrect = QRCodeEncoder. ERROR_CORRECTION.M; 49 // generate a QR code image 50 System. drawing. bitmap image = qrCodeEncoder. encode (codeNumber, Encoding. UTF8); 51 52 return image; 53} 54 55 public static string CreateLo GQrCode (string strContent) 56 {57 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder (); 58 qrCodeEncoder. QRCodeEncodeMode = QRCodeEncoder. ENCODE_MODE.BYTE; 59 qrCodeEncoder. QRCodeScale = 5; 60 qrCodeEncoder. QRCodeVersion = 8; 61 qrCodeEncoder. QRCodeErrorCorrect = QRCodeEncoder. ERROR_CORRECTION.M; 62 System. drawing. image image = qrCodeEncoder. encode (strContent, Encoding. UTF8); // generate a QR code image 63 64 System. I O. memoryStream MStream = new System. IO. memoryStream (); 65 image. save (MStream, System. drawing. imaging. imageFormat. png); 66 67 System. IO. memoryStream MStream1 = new System. IO. memoryStream (); 68 CombinImage (image, HttpRuntime. appDomainAppPath + "QrCodes/logo.png "). save (MStream1, System. drawing. imaging. imageFormat. png); 69 70 string reStr = Convert. toBase64String (MStream1.ToArray (); 71 // image. dispose (); 72 MStream. dispose (); 73 MStream1.Dispose (); 74 return reStr; 75} 76 77 public string SaveImg (Bitmap bmp, string path) 78 {79 bmp. save (path); 80 return path; 81} 82 83 // <summary> 84 // Save the image 85 // </summary> 86 // <param name = "bmp"> source </param> 87 // <param name = "path"> path </param> 88 // <param name = "jpg"> Format </param> 89 // <returns> </returns> 90 public bool SaveImg (Bitmap bmp, string path, ImgEnum jpg) 91 {92 try 93 {94 bmp. save (path); 95 return true; 96} 97 catch 98 {99 return false; 100} 101} 102 103 // <summary> 104 // Save the image 105 // </summary> 106 // <param name = "bmp"> source </param> 107 /// <param name = "path"> path </param> 108 // <param name = "path"> file name </param> 109 // /<returns> </returns> 110 public bool SaveImg (Bitmap bmp, string path, string fileName) 111 {112 try113 {114 bmp. save (path + fi LeName); 115 return true; 116} 117 catch118 {119 return false; 120} 121} 122 123 // <summary> 124 // Save the image 125 // </summary> 126 // <param name = "bmp"> source </param> 127 /// <param name = "path"> path </param> 128 // <param name = "path"> file name </param> 129 // /<param name = "jpg"> Format </param> 130 // <returns> </returns> 131 public bool SaveImg (Bitmap bmp, string path, string fileName, ImgEnum jpg) 132 {133 try134 {135 var ext = GetImgType (jpg); 136 bmp. save (path + fileName + ext); 137 return true; 138} 139 catch140 {141 return false; 142} 143} 144 145 // <summary> 146 // Bitmap converts base64 encoding 147 /// </summary> 148 // <param name = "bmp"> </param> 149 // <returns> </returns> 150 public string GetBase64String (Bitmap bmp) 151 {152 MemoryStream MS = new MemoryStream (); 153 bmp. save (MS, System. drawing. imaging. imageFormat. jpeg); 154 byte [] Arr = new byte [ms. length]; 155 ms. position = 0; 156 ms. read (arr, 0, (int) ms. length); 157 ms. close (); 158 string pic = Convert. toBase64String (arr); 159 return pic; 160} 161 162 // <summary> 163 // get the stream 164 // </summary> 165 // <param name = "bmp"> </param> 166 /// <returns> </returns> 167 public Stream GetStream (Bitmap bmp) 168 {169 MemoryStream MS = new MemoryStream (); 170 bmp. save (MS, System. drawing. imaging. im AgeFormat. jpeg); 171 byte [] arr = new byte [ms. length]; 172 ms. position = 0; 173 ms. read (arr, 0, (int) ms. length); 174 ms. close (); 175 return MS; 176} 177 178 179 // <summary> 180 // call this function to merge the two images, similar to album, has a 181 // background image, paste your target image in the middle 182 /// </summary> 183 /// <param name = "imgBack"> pasted source image </param> 184 /// <param name = "destImg"> pasted target Image </param> 185 // <summary> 186 public static Image CombinImage (Image imgBack, String destImg) 187 {188 Image img = Image. FromFile (destImg); // picture 189 if (img. Height! = 65 | img. Width! = 65) 190 {191 img = KiResizeImage (img, 65, 65, 0); 192} 193 Graphics g = Graphics. fromImage (imgBack); 194 195 GB. drawImage (imgBack, 0, 0, imgBack. width, imgBack. height); // g. drawImage (imgBack, 0, 0, width, height); 196 197 // g. fillRectangle (System. drawing. brushes. white, imgBack. width/2-img. width/2-1, imgBack. width/2-img. width/2-198, 1); // brush a black border around the photo 199 // g. drawImage (img, left margin of the photo and photo frame, top margin of the photo and photo frame, photo width, photo height); 200 201 GB. drawImage (img, imgBack. width/2-img. width/2, imgBack. width/2-img. width/2, img. width, img. height); 202 GC. collect (); 203 return imgBack; 204} 205 206 // <summary> 207 // Resize image 208 // </summary> 209 // <param name = "bmp"> original Bitmap </ param> 210 /// <param name = "newW"> New width </param> 211 /// <param name = "newH"> new height </param> 212 /// <param name = "Mode"> reserved, currently, images not processed using </param> 213 // <returns> </returns> 214 public static Image KiResizeImage (Image bmp, int newW, int newH, int Mode) 215 {216 try217 {218 Image B = new Bitmap (newW, newH); 219 Graphics g = Graphics. fromImage (B); 220 // The quality of the interpolation algorithm is 221 GB. interpolationMode = InterpolationMode. highQualityBicubic; 222g. drawImage (bmp, new Rectangle (0, 0, newW, newH), new Rectangle (0, 0, bmp. width, bmp. height), GraphicsUnit. pixel), 223 GB. dispose (); 224 return B; 225} 226 catch227 {228 return null; 229} 230} 231} 232}View Code

 

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.