1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| /// <summary> /// 生成用户名片 /// </summary> /// <returns></returns> public ActionResult creQRcode() { string name = Request["name"]==null? "暂无" : Request["name"]; string tel = Request["phone"] == null ? "400 XXXX 421" : Request["phone"]; string TITLE = Request["phone"] == null ? "400 XXXX 421" : Request["phone"]; string ORG = Request["userTypeName"] == null ? "暂无" : Request["userTypeName"]; string ADR = Request["address"] == null ? "XXXXX" : Request["address"]; string URL = "http://www.yhfy.cn/"; string EMAIL = Request["email"] == null ? "bXXXXX1@126.com" : Request["email"]; string comment = Request["comment"] == null ? "无" : Request["comment"];
StringBuilder card = new StringBuilder(); card.Append("BEGIN:VCARD"); card.Append("\r\nFN:" + name);//姓名 card.Append("\r\nTEL;CELL:" + tel);//手机号 card.Append("\r\nTITLE:" + TITLE);// card.Append("\r\nORG:" + ORG+ "-" + comment); card.Append("\r\nTEL;WORK:" + "400-6690421"); card.Append("\r\nADR;WORK:" + ADR); card.Append("\r\nURL:" + URL); card.Append("\r\nEMAIL;WORK:" + EMAIL); card.Append("\r\nEND:VCARD\r\n");
Bitmap bmp = GetTwoDimensionCode(card.ToString(), string.Empty, 200, 200, "微软雅黑");
string path = "~/Upload/MyQRCoed/"; string path2 = Request.MapPath(path); if (!Directory.Exists(path2)) { Directory.CreateDirectory(path2); } string guid = Guid.NewGuid().ToString(); string newPath = path2 + guid + ".jpg"; bmp.Save(newPath, ImageFormat.Jpeg); string src = "../Upload/MyQRCoed/" + guid + ".jpg"; return Content(src);
}
/// <summary> /// 绘制二维码 /// </summary> /// <param name="strSource"></param> /// <param name="text"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="fontName"></param> /// <returns></returns> public static Bitmap GetTwoDimensionCode(string strSource, string text, int width, int height, string fontName) { // 创建Bitmap对象 Bitmap bmp = new Bitmap(width, height); // 从image创建 Graphics对象 Graphics objGraphics = Graphics.FromImage(bmp); // 填上背景色 objGraphics.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height); // ThoughtWorks.QRCode.Codec.QRCodeEncoder qrCodeEncoder = new ThoughtWorks.QRCode.Codec.QRCodeEncoder(); // 设置编码方法 qrCodeEncoder.QRCodeEncodeMode = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE; // 设置大小 qrCodeEncoder.QRCodeScale = 3; // 适用于信息量较少的情形,图像越小保存的信息量越少 // qrCodeEncoder.QRCodeScale = 4; // 设置版本 qrCodeEncoder.QRCodeVersion = 0; // 设置错误校验的级别,正因为二维码有纠错能力,才能够加入logo qrCodeEncoder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M; Image image = qrCodeEncoder.Encode(strSource, Encoding.GetEncoding("utf-8")); // 写入二维码 int x = (int)(width - image.Width) / 2; int y = (int)(height - image.Height) / 2; objGraphics.DrawImage(image, new Point(x, y)); // 添加Logo图标 image = MvcTMM.Properties.Resources.logo; x = (int)(width - image.Width) / 2; y = (int)(height - image.Height) / 2; objGraphics.DrawImage(image, new Point(x, y)); // 写入字符串 //objGraphics.DrawString(text, new Font(fontName, 13, FontStyle.Bold), // Brushes.Black, new PointF(43, 15)); return bmp; }
|