生成方法是调用外网API
为了不直接调用别人的接口 ,我们创建一个 QrImg.aspx 用于显示二维码,传参数即可
QR类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- /// <summary>
- /// 调用外网API 生成二维码 周祥 2013年11月12日10:54:38
- /// </summary>
- public class Qr
- {
- public Qr()
- {
- //
- //TODO: 在此处添加构造函数逻辑
- //
- }
- /*
- bg 背景颜色 bg=颜色代码,例如:bg=ffffff
- fg 前景颜色 fg=颜色代码,例如:fg=cc0000
- gc 渐变颜色 gc=颜色代码,例如:gc=cc00000
- el 纠错等级 el可用值:h\q\m\l,例如: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";
- /// <summary>
- /// 根据URL和参数 返回地址
- /// </summary>
- /// <param name="name"></param>
- /// <param name="tel"></param>
- /// <param name="email"></param>
- /// <param name="logo"></param>
- /// <returns></returns>
- public static string GenerationCard(string name, string tel, string email, string logo = "")
- {
- string RequestUrl = api;
- string mecard = "MECARD:N:" + name + ";TEL:" + tel + ";EMAIL:" + email + ";";
- RequestUrl += "?text=" + mecard;
- if (logo != "")
- {
- RequestUrl += "&logo=" + logo;
- }
- return RequestUrl;
- }
- }
复制代码 QrImg.aspx.cs- 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 QrImg : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Request["name"] != null)
- {
- try
- {
- string url = Qr.GenerationCard(Request["name"],Request["tel"], Request["email"]);
- WebRequest wreq = WebRequest.Create(url);
- wreq.Timeout = 10000;
- HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
- Stream s = wresp.GetResponseStream();
- System.IO.MemoryStream ms = new System.IO.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)
- {
- //报错不做任何处理
- }
- }
- }
- }
复制代码 QrImg.aspx- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="QrImg.aspx.cs" Inherits="QrImg" %>
复制代码
|
|