using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
namespace Mtxfw.VipSite
{
///
/// 验证码 by Telesa 2011.04.07
///
public class CheckCode0 : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
if (!String.IsNullOrEmpty(context.Request["yzcode"]))
{
string yzcode = HttpUtility.UrlDecode(context.Request["yzcode"].ToString()).Replace("_","+");
//Mtxfw.Utility.Common.WriteHtml("/weixin/yzcode.txt", yzcode);
yzcode = Mtxfw.Utility.Security.DecryptString(yzcode);
//Mtxfw.Utility.Common.WriteHtml("/weixin/yzcode0.txt", yzcode);
ValidateCode(yzcode, 60, 25, "Arial", 12, "#FFFFFF");
}
}
///
/// 生成图片并写入字符
///
/// 目标字符
/// 宽
/// 高
/// 字体文件
/// 字体大小
/// 图片背景颜色
private void ValidateCode(string VNum, int w, int h, string font, int fontSize, string bgColor)
{
//生成图像的实例
Bitmap Img = new Bitmap(w, h);
//从Img对象生成新的Graphics对象
Graphics g = Graphics.FromImage(Img);
//背景颜色
g.Clear(ColorTranslator.FromHtml(bgColor));
//生成Font类的实例
Font f = new Font(font, fontSize, FontStyle.Bold);
//生成笔刷类的实例
SolidBrush s = new SolidBrush(Color.Red);
//将VNum写入图片中
g.DrawString(VNum, f, s, 3, 3);
//将此图像以Jpeg图像文件的格式保存到流中
Img.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);
System.Web.HttpContext.Current.Response.ContentType = "image/Jpeg";
g.Dispose();
Img.Dispose();
System.Web.HttpContext.Current.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}