关闭

asp.net验证码

119人阅读 评论(0) 收藏 举报

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

public partial class validate : System.Web.UI.Page
{
Random rdm = new Random(unchecked((int)DateTime.Now.Ticks));

protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
MemoryStream My_Stream = new MemoryStream();
My_Stream = Get_Images(11, 4, "#ffffff");//这里是传的参数,
Show_image(My_Stream);
My_Stream.Close();
}

private MemoryStream Get_Images(int Font_Size, int Char_Number, string BackgroundColor)
{

int image_w = Convert.ToInt32(Font_Size * 1.3) + Font_Size * Char_Number;
int image_h = Convert.ToInt32(1.8 * Font_Size);
Bitmap Temp_Bitmap;//'封装GDI+位图
Graphics Temp_Graphics;//封装GDI+绘图面
Color Color_Back = ColorTranslator.FromHtml(BackgroundColor);//'背景颜色
Temp_Bitmap = new Bitmap(image_w, image_h, PixelFormat.Format32bppRgb);//注意注 确定背景大小
Temp_Graphics = Graphics.FromImage(Temp_Bitmap);
Temp_Graphics.FillRectangle(new SolidBrush(Color_Back), new Rectangle(0, 0, image_w, image_h));//注意注 绘制背景
string Sesson_Company = "";//为了进行验证比较
Random random = new Random();
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(Temp_Bitmap.Width);
int x2 = random.Next(Temp_Bitmap.Width);
int y1 = random.Next(Temp_Bitmap.Height);
int y2 = random.Next(Temp_Bitmap.Height);
Temp_Graphics.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}

for (int n = 0; n < Char_Number; n++)
{
string Show_Str = getChar();//要显示为图像的字符
Sesson_Company = Sesson_Company + Show_Str;
//    int Show_Str_Font_Size = Convert.ToInt32(3 * rdm.NextDouble()+(Font_Size-1));//'字体随机大小 下波动1
int Show_Str_Font_Size = Convert.ToInt32(Font_Size);

Color Color_Font = ColorTranslator.FromHtml(getColor());//'字体随机颜色
string Show_Font_Name = getFont();//'字体
Font Show_Str_Font = new Font(Show_Font_Name, Show_Str_Font_Size, FontStyle.Bold);//'定义文本格式(字体,字号,粗体)
Temp_Graphics.DrawString(Show_Str, Show_Str_Font, new SolidBrush(Color_Font), Convert.ToInt32(Font_Size / 2) + n * Font_Size, Convert.ToInt32(image_h * 0.125));//'绘出字符 '绘字符的Y方向下波动+4
}

for (int i = 0; i < 100; i++)
{
int x = random.Next(Temp_Bitmap.Width);
int y = random.Next(Temp_Bitmap.Height);
Temp_Bitmap.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
Temp_Graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, Temp_Bitmap.Width - 1, Temp_Bitmap.Height - 1);

//保存到Session
Session.Clear();
Session["ImageNumber"] = Sesson_Company.Trim();

MemoryStream Temp_Stream = new MemoryStream();
Temp_Bitmap.Save(Temp_Stream, ImageFormat.Jpeg);
Temp_Graphics.Dispose();//'释放资源
Temp_Bitmap.Dispose();//'释放资源
Temp_Stream.Close();//'关闭打开的流文件
return Temp_Stream;//'返回流
}

private void Show_image(MemoryStream Show_Stream)//'显示内存图像
{

Response.ClearContent();
Response.ContentType = "Image/Jpeg";
Response.BinaryWrite(Show_Stream.ToArray());
Response.End();
}

private string getChar()//'获得随机字符 1-9 a-z A-Z
{
char[] Char_array = new char[5];
Char_array[0] = Convert.ToChar(rdm.Next(9) + 49);

//    Char_array[1] = Convert.ToChar(rdm.Next(26)+65);
//    Char_array[2] = Convert.ToChar(rdm.Next(26)+65);
//    Char_array[3] = Convert.ToChar(rdm.Next(26)+65);
Char_array[1] = Convert.ToChar(rdm.Next(9) + 49);
Char_array[2] = Convert.ToChar(rdm.Next(9) + 49);
Char_array[3] = Convert.ToChar(rdm.Next(9) + 49);

Char_array[4] = Convert.ToChar(rdm.Next(9) + 49);
//Char_array[4] = Convert.ToChar(rdm.Next(26)+97);
//return Char_array[Convert.ToInt32(5 * rdm.NextDouble())];
//return Char_array[Convert.ToInt32(rdm.Next(4))];
return Char_array[Convert.ToInt32(rdm.Next(4))].ToString();
}

private string getColor()//'获得随机颜色
{
int int_a, int_b, int_c;
int_a = Convert.ToInt32(180 * rdm.NextDouble() + 20);
int_b = Convert.ToInt32(180 * rdm.NextDouble() + 20);
int_c = Convert.ToInt32(180 * rdm.NextDouble() + 20);
if (int_a > 150 && int_b > 150 && int_c > 150) int_a = Convert.ToInt32(150 * rdm.NextDouble() + 20);
return "#" + Convert.ToString(int_a, 16) + Convert.ToString(int_b, 16) + Convert.ToString(int_c, 16);
}

private string getFont()//'获得随机字体
{
return "Fixedsys";
/*
string[] font_array=new string[7];
font_array[0] = "Book Antiqua";
font_array[1] = "Microsoft Sans Serif";
font_array[2] = "Comic Sans MS";
font_array[3] = "Comic Sans MS";
font_array[4] = "Comic Sans MS";
font_array[5] = "Comic Sans MS";
font_array[6] = "Comic Sans MS";
return font_array[Convert.ToInt32(rdm.Next(6))];
*/
}

private string getFont1()//'获得随机字体
{
return "宋体";
/*
string[] font_array=new string[10];
font_array[0] = "华文行楷";
font_array[1] = "隶书";
font_array[2] = "华文彩云";
font_array[3] = "方正舒体";
font_array[4] = "华文彩云";
font_array[5] = "方正舒体";
font_array[6] = "华文彩云";
font_array[7] = "方正舒体";
font_array[8] = "楷体_GB2312";
font_array[9] = "华文行楷";
return font_array[Convert.ToInt32(rdm.Next(9))];
*/
}
}

 

在前台页面
<script type="text/javascript" language="javascript">
function doChange() {
document.getElementById("ig").src = "validate.aspx?img=" + Math.random();
}
</script>

<img id="ig" onclick="doChange()" alt="点击换图" src="validate.aspx" /><asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>

 

添加按钮和对应的事件,在事件中进行判断,使用Session["ImageNumber"]与用户输入的进行比较

本文链接:http://www.snowdi.com/633.html → 转载请注明文章出自雪頔网

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11890次
    • 积分:286
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:9篇
    • 译文:0篇
    • 评论:0条
    文章存档