使用实例:
private string urlPath = ""; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { urlPath = Server.MapPath("~/file/"); qrCodeInfo(); } } /// <summary> /// 二维码生成 /// </summary> private void qrCodeInfo() { Response.Write("<IMG alt=\"" + "" + "\" src=\"" + "file/" + QrCodeHelper.getQrCodePath("https://www.zhengdecai.com/", "8", urlPath, "zhengdecai_qr.png") + "\" border=\"0\">" + " "); Response.Write("<IMG alt=\"" + "" + "\" src=\"" + "file/" + QrCodeHelper.getQrCodePath("https://www.zhengdecai.com/", "8", urlPath + "zhengdecai_logo.png", urlPath, "zhengdecai_qr_logo.png") + "\" border=\"0\">" + "<br />"); }
类库信息:
/// <summary> ///二维码生成 /// </summary> public class QrCodeHelper { #region 二维码生成 /// <summary> /// 生成二维码图片 /// </summary> /// <param name="Url">二维码地址</param> /// <param name="txt_size">生成图片大小</param> /// <param name="filepath">生成后的存放路径</param> /// <param name="filename">生成后的文件名</param> /// <returns>返回生成后的图片文件名</returns> public static string getQrCodePath(string Url, string txt_size, string filepath, string filename) { string txt_qr = ConverToGB(Url, 16); string qrEncoding = "Byte"; string Level = "M"; string txt_ver = "7"; 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) { return ""; } try { int version = Convert.ToInt16(txt_ver); qrCodeEncoder.QRCodeVersion = version; } catch (Exception ex) { return ""; } 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; Bitmap image; String data = txt_qr; image = qrCodeEncoder.Encode(data); System.IO.FileStream fs = new System.IO.FileStream(filepath + filename, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); fs.Close(); image.Dispose(); if (File.Exists(filepath + filename) == true) //判断是否已经上传 { return filename; } else { return ""; } } /// <summary> /// 生成二维码图片 /// </summary> /// <param name="Url">二维码地址</param> /// <param name="txt_size">生成图片大小</param> /// <param name="logopath">Logo图片路径</param> /// <param name="filepath">生成后的存放路径</param> /// <param name="filename">生成后的文件名</param> /// <returns>返回生成后的图片文件名</returns> public static string getQrCodePath(string Url, string txt_size, string logopath, string filepath, string filename) { string fileAbsPath = logopath; string txt_qr = ConverToGB(Url, 16); string qrEncoding = "Byte"; string Level = "M"; string txt_ver = "7"; QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); String encoding = qrEncoding; if (encoding == "Byte") { qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; } try { int scale = Convert.ToInt16(txt_size); qrCodeEncoder.QRCodeScale = scale; } catch (Exception ex) { return ""; } try { int version = Convert.ToInt16(txt_ver); qrCodeEncoder.QRCodeVersion = version; } catch (Exception ex) { return ""; } string errorCorrect = Level; if (errorCorrect == "L") qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L; Bitmap image; String data = txt_qr; image = qrCodeEncoder.Encode(data); System.IO.FileStream fs = new System.IO.FileStream(filepath + filename, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); fs.Close(); image.Dispose(); int resultVal = (int)ReturnVal.Succeed; if (resultVal == 1) { string imgFile = ImageWatermark(filepath + filename, fileAbsPath, 30); if (File.Exists(imgFile)) { File.Delete(imgFile); } } if (File.Exists(filepath + filename) == true) //判断是否已经上传 { return filename; } else { return ""; } } #endregion #region 二维码生成辅助 #region 返回值 /// <summary> /// 返回值 /// </summary> private enum ReturnVal : int { /// <summary> /// 不能上传 0 K大小的文件 /// </summary> FileEmpty = -2, /// <summary> /// 未选中文件 /// </summary> NotSelected = -1, /// <summary> /// 上传失败 /// </summary> Failed = 0, /// <summary> /// 成功 /// </summary> Succeed = 1 } #endregion /// <summary> /// 10进制或16进制转换为中文 /// </summary> /// <param name="name">要转换的字符串</param> /// <param name="fromBase">进制(10或16)</param> /// <returns></returns> public static string ConverToGB(string text, int fromBase) { string value = text; MatchCollection mc; System.Text.StringBuilder sb = new System.Text.StringBuilder(); switch (fromBase) { case 10: MatchCollection mc1 = Regex.Matches(text, @"&#([\d]{5})", RegexOptions.Compiled | RegexOptions.IgnoreCase); foreach (Match _v in mc1) { string w = _v.Value.Substring(2); w = Convert.ToString(int.Parse(w), 16); byte[] c = new byte[2]; string ss = w.Substring(0, 2); int c1 = Convert.ToInt32(w.Substring(0, 2), 16); int c2 = Convert.ToInt32(w.Substring(2), 16); c[0] = (byte)c2; c[1] = (byte)c1; sb.Append(Encoding.Unicode.GetString(c)); } value = sb.ToString(); break; case 16: mc = Regex.Matches(text, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase); if (mc != null && mc.Count > 0) { foreach (Match m2 in mc) { string v = m2.Value; string w = v.Substring(2); byte[] c = new byte[2]; int c1 = Convert.ToInt32(w.Substring(0, 2), 16); int c2 = Convert.ToInt32(w.Substring(2), 16); c[0] = (byte)c2; c[1] = (byte)c1; sb.Append(Encoding.Unicode.GetString(c)); } value = sb.ToString(); } break; } return value; } /// <summary> /// 图片水印处理方法 /// </summary> /// <param name="path">需要加载水印的图片路径(绝对路径)</param> /// <param name="waterpath">水印图片(绝对路径)</param> /// <param name="waterpath">水印图片最大宽度</param> /// <returns></returns> private static string ImageWatermark(string path, string waterpath, int maxW) { //获取文件扩展名 string kz_name = Path.GetExtension(path); string fileNames = ""; //暂时只支持给.JPG,.BMP,.JPEG格式加水印 if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg" || kz_name == ".png" || kz_name == ".gif") { //设置新的文件名 DateTime time = DateTime.Now; string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString(); //加载需要加载水印的图片 Image img = Bitmap.FromFile(path); //加载水印图片 Image waterimg = Image.FromFile(waterpath); //添加水印 Graphics g = Graphics.FromImage(img); //获取水印位置设置 ArrayList loca = new ArrayList(); int x = 0; int y = 0; int width = maxW; int height = waterimg.Height / (waterimg.Width / maxW); int cH = 0; if (height < maxW) { cH = maxW - height; } x = img.Width / 2 - width / 2; y = img.Height / 2 - height / 2 + cH / 2; loca.Add(x); loca.Add(y); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; //这里是矩形框居中 sf.LineAlignment = StringAlignment.Center; //这里是文本居中 g.DrawImage(waterimg, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), width, height)); //释放资源 waterimg.Dispose(); g.Dispose(); //保存水印图片 string newpath = Path.GetDirectoryName(path) + "\\" + filename + kz_name; fileNames = filename + kz_name; img.Save(newpath); img.Dispose(); //将水印复制到原有图片 //将水印图片替换原有图片 File.Copy(newpath, path, true); //删除水印 if (File.Exists(newpath)) { File.Delete(newpath); } } else { fileNames = ""; } return fileNames; } #endregion }
显示效果:
在生成二维码时候,需要引用“ThoughtWorks.QRCode.dll”,这个可以请教度娘
以上类库内容来源互联网,站长稍作整理
评论列表: