asp.net(C#)利用QRCode生成二维码---.NET菜鸟的成长之路

<%@ 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 = ;
qrCodeEncoder.QRCodeVersion = ;
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;
}
}
}

续集:

<%@ 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;
using System.Drawing;
using System.Drawing.Drawing2D; 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.Image image = qrCodeEncoder.Encode(data);
System.IO.MemoryStream MStream = new System.IO.MemoryStream();
image.Save(MStream, System.Drawing.Imaging.ImageFormat.Png); System.IO.MemoryStream MStream1 = new System.IO.MemoryStream();
CombinImage(image, context.Server.MapPath("~/images/201292891051540.jpg")).Save(MStream1, System.Drawing.Imaging.ImageFormat.Png);
context.Response.ClearContent();
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(MStream1.ToArray()); //image.Dispose();
MStream.Dispose();
MStream1.Dispose();
} context.Response.Flush();
context.Response.End();
} /// <summary>
/// 调用此函数后使此两种图片合并,类似相册,有个
/// 背景图,中间贴自己的目标图片
/// </summary>
/// <param name="imgBack">粘贴的源图片</param>
/// <param name="destImg">粘贴的目标图片</param>
public static Image CombinImage(Image imgBack, string destImg)
{
Image img = Image.FromFile(destImg); //照片图片
if (img.Height != 65 || img.Width != 65)
{
img = KiResizeImage(img, 65, 65, 0);
}
Graphics g = Graphics.FromImage(imgBack); g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高); //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框 //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高); g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);
GC.Collect();
return imgBack;
} /// <summary>
/// Resize图片
/// </summary>
/// <param name="bmp">原始Bitmap</param>
/// <param name="newW">新的宽度</param>
/// <param name="newH">新的高度</param>
/// <param name="Mode">保留着,暂时未用</param>
/// <returns>处理以后的图片</returns>
public static Image KiResizeImage(Image bmp, int newW, int newH, int Mode)
{
try
{
Image b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
{
return null;
}
} public bool IsReusable
{
get
{
return false;
}
}
}
上一篇:asp.net(C#)利用QRCode生成二维码(续)-在二维码图片中心加Logo或图像


下一篇:hdu3746