/// <summary> /// 创建临时二维码画布 /// </summary> /// <param name="content">二维码内容</param> /// <returns></returns> public static Bitmap CreateTempQRCode(string content) { GDataBase.MyGT gt = new GDataBase.MyGT(); if (string.IsNullOrEmpty(content)) { return(null); } string filename = string.Empty; string filepath = string.Empty; string txt_qr = content; string qrEncoding = "Byte"; string Level = "L"; string txt_ver = "10"; string txt_size = "10"; QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); String encoding = qrEncoding; if (encoding == "Byte") { qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; } else if (encoding == "AlphaNumeric") { qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC; } else if (encoding == "Numeric") { qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC; } try { int scale = Convert.ToInt16(txt_size); qrCodeEncoder.QRCodeScale = scale; } catch (Exception ex) { gt.WriteFile(ex.Message); } try { int version = Convert.ToInt16(txt_ver); qrCodeEncoder.QRCodeVersion = version; } catch (Exception ex) { gt.WriteFile(ex.Message); } qrCodeEncoder.QRCodeScale = 4; qrCodeEncoder.QRCodeVersion = 0; string errorCorrect = Level; if (errorCorrect == "L") { qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L; } else if (errorCorrect == "M") { qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; } else if (errorCorrect == "Q") { qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q; } else if (errorCorrect == "H") { qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; } String data = txt_qr; return(qrCodeEncoder.Encode(data)); }
/// <summary> /// 创建并保存到本地 /// </summary> /// <param name="content">二维码内容</param> /// <param name="path">保存路径</param> /// <param name="imagename">返回图片名</param> /// <returns></returns> public static string CreateQRCode(string content, string path, ref string imagename) { GDataBase.MyGT gt = new GDataBase.MyGT(); if (string.IsNullOrEmpty(content)) { return(""); } string filename = string.Empty; string filepath = string.Empty; string txt_qr = content; string qrEncoding = "Byte"; string Level = "L"; string txt_ver = "10"; string txt_size = "10"; QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); String encoding = qrEncoding; if (encoding == "Byte") { qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; } else if (encoding == "AlphaNumeric") { qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC; } else if (encoding == "Numeric") { qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC; } try { int scale = Convert.ToInt16(txt_size); qrCodeEncoder.QRCodeScale = scale; } catch (Exception ex) { gt.WriteFile("生成二维码错误!"); } try { int version = Convert.ToInt16(txt_ver); qrCodeEncoder.QRCodeVersion = version; } catch (Exception ex) { gt.WriteFile("生成二维码错误!"); } qrCodeEncoder.QRCodeScale = 4; qrCodeEncoder.QRCodeVersion = 0; string errorCorrect = Level; if (errorCorrect == "L") { qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L; } else if (errorCorrect == "M") { qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; } else if (errorCorrect == "Q") { qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q; } else if (errorCorrect == "H") { qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string QRStr = string.Empty; if (string.IsNullOrEmpty(path)) { QRStr = "CreateErr"; } else { QRStr = DateTime.Now.ToString("yyyyMMddhhmmssffff"); } filename = QRStr + ".jpg"; System.Drawing.Image image; String data = txt_qr; image = qrCodeEncoder.Encode(data); filepath = path + "\\" + filename; System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); fs.Close(); image.Dispose(); imagename = filename; return(path + "/" + filename); }