代码修改后的版本,全部提交
This commit is contained in:
99
Mtxfw.shop/CheckCode.ashx.cs
Normal file
99
Mtxfw.shop/CheckCode.ashx.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
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.shop
|
||||
{
|
||||
/// <summary>
|
||||
/// 验证码 by Telesa 2011.04.07
|
||||
/// </summary>
|
||||
public class CheckCode : IHttpHandler, IRequiresSessionState
|
||||
{
|
||||
public void ProcessRequest(HttpContext context)
|
||||
{
|
||||
System.Web.HttpCookie hc = new HttpCookie("code");
|
||||
hc.Value = RandNum(4);
|
||||
System.Web.HttpContext.Current.Response.Cookies.Add(hc);
|
||||
|
||||
ValidateCode(hc.Value, 60, 25, "Arial", 12, "#FFFFFF");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成指定位数的随机数
|
||||
/// </summary>
|
||||
/// <param name="VcodeNum">参数是随机数的位数</param>
|
||||
/// <returns>返回一个随机数字符串</returns>
|
||||
public string RandNum(int VcodeNum)
|
||||
{
|
||||
string Vchar = "0,1,2,3,4,5,6,7,8,9";
|
||||
string[] VcArray = Vchar.Split(','); //拆分成数组
|
||||
string VNum = "";
|
||||
int temp = -1; //记录上次随机数值,尽量避避免生产几个一样的随机数
|
||||
|
||||
Random rand = new Random();
|
||||
//采用一个简单的算法以保证生成随机数的不同
|
||||
for (int i = 0; i < VcodeNum; i++)
|
||||
{
|
||||
if (temp != -1)
|
||||
{
|
||||
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
|
||||
}
|
||||
|
||||
int t = rand.Next(VcArray.Length - 1);
|
||||
if (temp != -1 && temp == t)
|
||||
{
|
||||
return RandNum(VcodeNum);
|
||||
}
|
||||
temp = t;
|
||||
VNum += VcArray[t];
|
||||
}
|
||||
return VNum;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成图片并写入字符
|
||||
/// </summary>
|
||||
/// <param name="VNum">目标字符</param>
|
||||
/// <param name="w">宽</param>
|
||||
/// <param name="h">高</param>
|
||||
/// <param name="font">字体文件</param>
|
||||
/// <param name="fontSize">字体大小</param>
|
||||
/// <param name="bgColor">图片背景颜色</param>
|
||||
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.Italic|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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user