asp.net(C#)利用QRCode生成二维码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="QRCode.aspx.cs" Inherits="QRCode" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>生成二维码</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        请输入内容:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br/>
        <asp:Button ID="Button1" runat="server" Text="生成二维码" onclick="Button1_Click" />
    </div>
    <div style="text-align:center">
        <asp:Image ID="ImageCode" runat="server" Visible="false" />
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class QRCode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string CodeText = TextBox1.Text.Trim();
        if (!string.IsNullOrEmpty(CodeText))
        {
            ImageCode.Visible = true;
            ImageCode.ImageUrl = "GetQRCode.ashx?CodeText=" + CodeText;
        }
    }
}

<%@ WebHandler Language="C#" Class="GetQRCode" %>

using System;
using System.Web;

using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
using System.IO;
using System.Text;

public class GetQRCode : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {

        String data = context.Request["CodeText"];
        if (!string.IsNullOrEmpty(data))
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale = 4;
            qrCodeEncoder.QRCodeVersion = 8;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            System.Drawing.Bitmap image = qrCodeEncoder.Encode(data);
            System.IO.MemoryStream MStream = new System.IO.MemoryStream();
            image.Save(MStream, System.Drawing.Imaging.ImageFormat.Png);
            context.Response.ClearContent();
            context.Response.ContentType = "image/Png";
            context.Response.BinaryWrite(MStream.ToArray());
        }

        context.Response.Flush();
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载,如文章对您有帮助,请页面左侧随意打赏。 https://blog.csdn.net/smartsmile2012/article/details/17069329
个人分类: Web Asp.net & C#
上一篇对Jquery中的ajax再封装,简化操作
下一篇SQL SERVER 查看日志大小及日志已满的处理方法 (转)
想对作者说点什么? 我来说一句

Asp.net生成二维码和解析二维码

2014年04月19日 955KB 下载

ASP生成二维码代码

2011年08月18日 732KB 下载

没有更多推荐了,返回首页

关闭
关闭