电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

ValidNumber.ashx验证码


发布日期:2023/3/10
 
ValidNumberashx 代码

<%@ WebHandler Language=C# Class=ValidNumber %>

using System;

using SystemWeb;

using SystemWebSessionState; //第一步导入此命名空间

public class ValidNumber : IHttpHandler IRequiresSessionState

{

public void ProcessRequest(HttpContext context)

{

//种子

string[] strlist = new[]

{

abcdqwertyuiopzxnvWQBADDFGHJKL

};

//生成随机数

string[] list = new string[];

Random rd = new Random(DateTimeNowMillisecond *

for (int i = ; i < ; i++)

{

list[i] = strlist[rdNext(strlistLength)];

SystemThreadingThreadSleep(

}

SystemTextStringBuilder sb = new SystemTextStringBuilder()

for (int i = ; i < ; i++)

{

sb = sbAppend(list[i])

}

HttpContextCurrentSession[validNumber] = sb;

//创建一张图片

SystemDrawingBitmap image = new SystemDrawingBitmap(listLength *

//创建画布

SystemDrawingGraphics g = SystemDrawingGraphicsFromImage(image)

//清空背景色并添充成白色

gClear(SystemDrawingColorWhite)

//噪声线

for (int i = ; i < ; i++)

{

Random rds = new Random()

float x = rdsNext(imageWidth

float x = rdsNext(imageWidth

float y = rdsNext(imageHeight

float y = rdsNext(imageHeight

SystemThreadingThreadSleep(

gDrawLine(new SystemDrawingPen(SystemDrawingColorSilver) x y x y

}

SystemDrawingFont f = new SystemDrawingFont(黑体 SystemDrawingGraphicsUnitPixel)

SystemDrawingBrush bs = new SystemDrawingSolidBrush(SystemDrawingColorRed)

gDrawString(sbToString() f bs

//当前请求输出的类型

HttpContextCurrentResponseContentType = image/jpeg;

//保存到输出流

imageSave(HttpContextCurrentResponseOutputStream SystemDrawingImagingImageFormatJpeg)

gDispose()

imageDispose()

}

public bool IsReusable

{

get

{

return false;

}

}

}

页面调用时 <img src=ValidNumberashx alt=图片验证码 /> 但是回发session值改变页面图片没变暂无比较理想的解决办法提交时后台用 ResponseWrite(<script> windowlocationhref=windowlocationhref</script>) 刷新下页面

上一篇:背景图片的定位问题详解

下一篇:怎么样将数组写入File Mapping