A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sunrise2 高级黑马   /  2014-8-11 09:26  /  921 人查看  /  0 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

   生成方法是调用外网API   
  为了不直接调用别人的接口 ,我们创建一个 QrImg.aspx 用于显示二维码,传参数即可

QR类
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;

  5. /// <summary>
  6. /// 调用外网API 生成二维码  周祥 2013年11月12日10:54:38
  7. /// </summary>
  8. public class Qr
  9. {
  10.     public Qr()
  11.     {
  12.         //
  13.         //TODO: 在此处添加构造函数逻辑
  14.         //
  15.     }
  16.     /*
  17.     bg  背景颜色    bg=颜色代码,例如:bg=ffffff
  18.     fg  前景颜色    fg=颜色代码,例如:fg=cc0000
  19.     gc  渐变颜色    gc=颜色代码,例如:gc=cc00000
  20.     el  纠错等级    el可用值:h\q\m\l,例如:el=h
  21.     w   尺寸大小    w=数值(像素),例如:w=300
  22.     m   静区(外边距) m=数值(像素),例如:m=30
  23.     pt  定位点颜色(外框)   pt=颜色代码,例如:pt=00ff00
  24.     inpt    定位点颜色(内点)   inpt=颜色代码,例如:inpt=000000
  25.     logo    logo图片  logo=图片地址,例如:logo=http://www.xxx.cn/logo.png
  26.     */
  27.     public const string api = "http://qr.liantu.com/api.php";
  28.     /// <summary>
  29.     /// 根据URL和参数 返回地址
  30.     /// </summary>
  31.     /// <param name="name"></param>
  32.     /// <param name="tel"></param>
  33.     /// <param name="email"></param>
  34.     /// <param name="logo"></param>
  35.     /// <returns></returns>
  36.     public static string GenerationCard(string name, string tel, string email, string logo = "")
  37.     {
  38.         string RequestUrl = api;
  39.         string mecard = "MECARD:N:" + name + ";TEL:" + tel + ";EMAIL:" + email + ";";
  40.         RequestUrl += "?text=" + mecard;
  41.         if (logo != "")
  42.         {
  43.             RequestUrl += "&logo=" + logo;
  44.         }
  45.         return RequestUrl;
  46.     }
  47. }
复制代码
QrImg.aspx.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Net;
  8. using System.IO;
  9. using System.Drawing.Imaging;

  10. public partial class QrImg : System.Web.UI.Page
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         if (Request["name"] != null)
  15.         {
  16.             try
  17.             {
  18.                 string url = Qr.GenerationCard(Request["name"],Request["tel"], Request["email"]);
  19.                 WebRequest wreq = WebRequest.Create(url);
  20.                 wreq.Timeout = 10000;
  21.                 HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
  22.                 Stream s = wresp.GetResponseStream();
  23.                 System.IO.MemoryStream ms = new System.IO.MemoryStream();
  24.                 System.Drawing.Image img = System.Drawing.Image.FromStream(s);
  25.                 img.Save(ms, ImageFormat.Gif);
  26.                 Response.ClearContent();
  27.                 Response.ContentType = "image/Gif";
  28.                 Response.BinaryWrite(ms.ToArray());
  29.                 img.Dispose();
  30.                 s.Dispose();
  31.             }
  32.             catch (Exception)
  33.             {
  34.                 //报错不做任何处理
  35.             }
  36.         }
  37.     }
  38. }
复制代码
QrImg.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="QrImg.aspx.cs" Inherits="QrImg" %>
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马