public class qr { public qr() { // //TODO: 在此处添加构造函数逻辑 // } /* bg 背景颜色 bg=颜色代码,例如:bg=ffffff fg 前景颜色 fg=颜色代码,例如:fg=cc0000 gc 渐变颜色 gc=颜色代码,例如:gc=cc00000 el 纠错等级 el可用值:hqml,例如:el=h w 尺寸大小 w=数值(像素),例如:w=300 m 静区(外边距) m=数值(像素),例如:m=30 pt 定位点颜色(外框) pt=颜色代码,例如:pt=00ff00 inpt 定位点颜色(内点) inpt=颜色代码,例如:inpt=000000 logo logo图片 logo=图片地址,例如:logo=http://www.xxx.cn/logo.png */ public const string api = "http://qr.liantu.com/api.php"; public static string GenerationCard(string name, string tel, string email, string logo = "") { string RequestUrl = api; string macard = "MECARD:N:" + name + ";TEL:" + tel + ";EMATL;" + email + ";"; RequestUrl += "?text=" + macard; if (logo != "") { RequestUrl += "&logo=" + logo; } return RequestUrl; } }
生成二维码后台
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.IO; using System.Drawing.Imaging; public partial class creatCode : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { string url = qr.GenerationCard(Request["name"], Request["tel"], Request["email"]); WebRequest wreq = WebRequest.Create(url); HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse(); Stream s = wresp.GetResponseStream(); System.IO.MemoryStream ms = new MemoryStream(); System.Drawing.Image img = System.Drawing.Image.FromStream(s); img.Save(ms, ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); img.Dispose(); s.Dispose(); } catch (Exception ex) { throw; } } } }
前台页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="creatCode.aspx.cs" Inherits="creatCode" %>
调用页面
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>生成验证码(演示)</title> </head> <body> <form action="creatCode.aspx" id="form1"> <label for="txtName"> 姓名:</label> <input type="text" name="txtName" /><br /> <label for="tel"> 电话: </label> <input type="text" name="tel" /><br /> <label for="email"> 邮箱: </label> <input type="text" name="email" /><br /> <input type="submit" name="submit" value="生成验证码" /> </form> <script type="text/javascript"> form1.submit.onclick = function () { var name = form1.name.value; var tel = form1.tel.value; var email = form1.email.value; window.location.href = "/creatCode.aspx?name=" + name + "&tel=" + tel + "&email=" + email; } </script> </body> </html>