using System; using System.Collections.Generic; using System.Text; //using Aspose.BarCode; using System.Drawing.Imaging; //using ThoughtWorks.QRCode.Codec; //using System.Drawing.Imaging; using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; using ThoughtWorks.QRCode.Codec.Util; namespace QrCode { public static class Qr { public static string toUrl2(string str) { try { Encoding utf8 = Encoding.UTF8; Encoding gb2312 = Encoding.GetEncoding("GB2312"); byte[] unicodeBytes = gb2312.GetBytes(str); byte[] asciiBytes = Encoding.Convert(gb2312, utf8, unicodeBytes); char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)]; utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0); string result = new string(asciiChars); return result; } catch { return ""; } } public static string toUrl(string str) { try { Encoding utf8 = Encoding.UTF8; Encoding gb2312 = Encoding.GetEncoding("GB2312"); byte[] unicodeBytes = utf8.GetBytes(str); byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes); char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, unicodeBytes.Length)]; utf8.GetChars(asciiBytes, 0, unicodeBytes.Length, asciiChars, 0); string result = new string(asciiChars); return result; } catch { return ""; } } public static bool OutQrInfo(string Info, string picFileName, out string errMsg) { errMsg = null; try { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrCodeEncoder.QRCodeScale = 4; qrCodeEncoder.QRCodeVersion = 8; qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; System.Drawing.Bitmap image = qrCodeEncoder.Encode(Info, Encoding.UTF8); string filepath= AppDomain.CurrentDomain.BaseDirectory; image.Save(filepath + "/QRCodeImg/" + picFileName); //, System.Drawing.Imaging.ImageFormat.Gif); return true; } catch (Exception te) { errMsg = te.Message; return false; } } //public static string toUrlX(string s) //{ // return s != null ? HttpContext.Current.Server.UrlEncode(s) : null; //} public static string OutQr4Url(string url, string caption) { string turl = string.Format("MEBKM:{0};URL:{1};", (caption), (url)); return turl; //string errMsg; //OutQrInfo(url, out errMsg); } public static string OutQr4SMS(string Mobile, string Info) { string url = string.Format("SMSTO:{0}:{1}", (Mobile), (Info)); return url; //string errMsg; //OutQrInfo(url, out errMsg); } public static string OutQr4MMS(string Mobile, string Info) { string url = string.Format("MMSTO:{0}:{1}", (Mobile), (Info)); return url; //string errMsg; //OutQrInfo(url, out errMsg); } public static string OutQr4Email(string Email, string Caption, string Body) { string url = string.Format("MATMSG:TO:{0};SUB:{1};BODY:{2};", (Email), (Caption), (Body)); return url; //string errMsg; //OutQrInfo(url, out errMsg); } public static string OutQr4WiFi(string WEP_WPA, string SID, string Password) { string url = string.Format("WIFI:T:{0};S:{1};P:{2};", (WEP_WPA), (SID), (Password)); return url; //string errMsg; //OutQrInfo(url, out errMsg); } public static string OutQr4Card(string ModelCode,string Factory, string Sex, string RimStyle, string Price) { string url = string.Format("GlasessModel: Code:{0};Factory:{3};nSex:{2};Rim style:{1};Price:{4};", (ModelCode), (Factory), (Sex), RimStyle, Price); return url; } public static string OutQr4Telephone(string Phone) { string url = string.Format("TEL:{0}", (Phone)); return url; //string errMsg; //OutQrInfo(url, out errMsg); } public static string OutQr4App(string AppID) { string url = string.Format("market://details?id={0}", toUrl(AppID)); return url; //string errMsg; //OutQrInfo(url, out errMsg); } public static string OutQr4Location(decimal longitude, decimal latitude, decimal accuracy) { string url = string.Format("GEO:{0},{1},{2}", longitude, latitude, accuracy); return url; // string errMsg; // OutQrInfo(url, out errMsg); //} } } }
以上是这个生成二维码的方法类。
下面是生成其中一个类型的方法:
string filename = DateTime.Now.ToFileTime().ToString()+".jpg"; string err = string.Empty; string s = Qr.OutQr4Card(txtModelCode.Text,txtFactory.Text,Sex,RimStyle,txtPrice.Text); Qr.OutQrInfo(s, filename, out err);
另外还要引用ThoughtWorks.QRCode.dll文件。