All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET,2.0高级数据处理之冲突检测_.net资料_编程技术
几个比较有用的正则表达式_php资料_编程技术
HTML 5 预览(1)_[Html教程]
不妨来做个尝试:UpdatePanel,for,ASP.NET,MVC_[Asp.Net教程]
浅谈.NET中的数据绑定表达式_[Asp.Net教程]
ASP.NET中web.config文件的配置_[Asp.Net教程]
Asp.Net中对Cookie的基本操作_.net资料_编程技术
构成Delphi工程的文件:工程文件
加速PHP动态网站,MySQL索引分析和优化_php资料_编程技术
微软.Net开发中的多线程编程总结_.net资料_编程技术
Reading number is top 10 pictures
寒流来了
真正的国产-非模拍 贵在是真实1
贩卖儿童者必须判死刑
Is said to be a Chinese female artist fame explicit pictures before2
The money of more than 100 countries and regions1
The world first motorcycle will be auctioned for 21000 pounds
Green sweet joey wong young old photos exposure
The money of more than 100 countries and regions6
HongMenYan premiere XinLiangGong clairvoyant outfit PK YiFeiLiu1
美女挤公交
Download software ranking
Tram sex maniac 2 (H) rar bag6
SQL2000 For 4IN1
SP4 for SQL2000
The hero
1400篇各类破解文章
豪门浪荡史
Unix video tutorial3
双旗镇刀客A
Tram sex maniac 2 (H) rar bag5
Jinling thirteen stock
delv published in(发表于) 2014/1/27 6:48:24 Edit(编辑)
Asp.Net图片验证码程序[含源码]_[Asp.Net教程]

Asp.Net图片验证码程序[含源码]_[Asp.Net教程]

Asp.Net图片验证码程序[含源码]_[Asp.Net教程]

Asp.Net图片验证码程序[含源码]


一、先看看效果:
http://www.wingoon.com首页登录口(大小为:75*21)图片验证码
http://app.wingoon.com/job/member/member_login.aspx?requestUrl=/job/member/index.aspx(大小为:100*32)


制作步骤:
(1)准备你想要的多张图片(数量不限,由你自己决定),将它们放在一个统一的目录下,比如我这里是“ValidateCodeImg”。图片尺寸尽量适合你的验证码尺寸。这样有利于达到最佳性能比。

(2)将以下代码分别COPY到你的工程目录下(记得放在ValidateCodeImg目录的上一级目录中)。

二、相关代码:
1、
// ValidateCode.aspx页面:
-----------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidateCode.aspx.cs" Inherits="Comm_ValidateCode" %>





生成图片验证码












2、// ValidateCode.aspx.cs
--------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;


public partial class Comm_ValidateCode : System.Web.UI.Page
{
int _width = 75;
int _height = 21;
// int _width = 128;
// int _height = 40;
string imgDir = @"ValidateCodeImg";
public int Width
{
get
{
return _width;
}
set
{
_width = value;
}
}


public int Height
{
get
{
return _height;
}
set
{
_height = value;
}
}


protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["w"] != null)
{
try
{
this._width = int.Parse(Request.QueryString["w"].Trim());
}
catch (Exception exc)
{
//
}
}
if (Request.QueryString["h"] != null)
{
try
{
this._height = int.Parse(Request.QueryString["h"].Trim());
}
catch (Exception exc)
{
//
}
}
// 4位数字的验证码
if (!IsPostBack)
{
string str_ValidateCode = GetRandomNumberString(4);
//用于验证的Session
Session["ValidateCode"] = str_ValidateCode;
CreateImage(str_ValidateCode);
}
}


// 生成随机数字字符串
public string GetRandomNumberString(int int_NumberLength)
{
string str_Number = string.Empty;
Random theRandomNumber = new Random();


for (int int_index = 0; int_index < int_NumberLength; int_index++)
str_Number += theRandomNumber.Next(10).ToString();


return str_Number;
}


设计家园 收集整理
//生成随机颜色
public Color GetRandomColor()
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(RandomNum_First.Next(50));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
int_Blue = (int_Blue > 255) ? 255 : int_Blue;


return Color.FromArgb(int_Red, int_Green, int_Blue);
}


public FileInfo[] GetAllFilesInPath(string path)
{
System.IO.DirectoryInfo di = new DirectoryInfo(path);


return di.GetFiles("*.jpg", SearchOption.TopDirectoryOnly);
}


public string GetRandomFile(string path)
{
FileInfo[] fi = this.GetAllFilesInPath(path);
Random rand = new Random(new Guid().GetHashCode() + (int)DateTime.Now.Ticks);
int k = rand.Next(0, fi.Length);


return fi[k].FullName;
}


public int GetRandomAngle()
{
Random rand = new Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(rand.Next(50));
return rand.Next(-45, 45);
}


//根据验证字符串生成最终图象
public void CreateImage(string str_ValidateCode)
{
//int int_ImageWidth = str_ValidateCode.Length * 13;
//int width = int_ImageWidth;
int int_ImageWidth = this.Width;
int width = int_ImageWidth;


int height = this.Height;


string filePath = Server.MapPath(imgDir);
Bitmap bgImg = (Bitmap)Bitmap.FromFile(GetRandomFile(filePath));


Random newRandom = new Random();
// 图高20px
Bitmap theBitmap = new Bitmap(width, height);
Graphics theGraphics = Graphics.FromImage(theBitmap);
theGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
theGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
theGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
theGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// 白色背景
//theGraphics.Clear(Color.White);
theGraphics.DrawImage(bgImg, new Rectangle(0, 0, width, height), new Rectangle(0, 0, bgImg.Width, bgImg.Height), GraphicsUnit.Pixel);
// 灰色边框
theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, height - 1);


//13pt的字体
float fontSize = this.Height * 1.0f / 1.38f;
float fontSpace = fontSize / 7f;
Font theFont = new Font("Arial", fontSize);
System.Drawing.Drawing2D.GraphicsPath gp = null;
System.Drawing.Drawing2D.Matrix matrix;
for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
{
string str_char = str_ValidateCode.Substring(int_index, 1);
Brush newBrush = new SolidBrush(GetRandomColor());
Point thePos = new Point((int)(int_index * (fontSize + fontSpace) + newRandom.Next(3)), 1 + newRandom.Next(3));
gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddString(str_char, theFont.FontFamily, 0, fontSize, thePos, new StringFormat());
matrix = new System.Drawing.Drawing2D.Matrix();
int angle = GetRandomAngle();
PointF centerPoint = new PointF(thePos.X + fontSize / 2, thePos.Y + fontSize / 2);
matrix.RotateAt(angle, centerPoint);
theGraphics.Transform = matrix;
theGraphics.DrawPath(new Pen(Color.White, 2f), gp);
//theGraphics.FillPath(new SolidBrush(Color.Black), gp);
theGraphics.FillPath(new SolidBrush(GetRandomColor()), gp);
theGraphics.ResetTransform();
}


if (gp != null) gp.Dispose();


// 将生成的图片发回客户端
MemoryStream ms = new MemoryStream();
theBitmap.Save(ms, ImageFormat.Png);


Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
theGraphics.Dispose();
theBitmap.Dispose();
Response.End();
}
}

三、如何使用?
1、引用验证码图形时:
&nbsp; onclick="Javascript:var now=new Date();var number = now.getSeconds(); document.getElementById('ValidateCodeAuto').src='ValidateCode.aspx?w=100&h=32&dd=' + number;">看不清楚
2、验证时,判断用户输入验证码是否等于 Session["ValidateCode"] 即可。
3、其他说明:(1)上面有两个参数w和h,是用来定义图形验证码大小的。比如你想将你验证码设置为128*40时,你只需要ValidateCode.aspx?w=128&h=40,然后将图片引用后面的Width,Height改成:width="128" height="40" 即可。对应的,看不清楚的Javascript代码也需要将src='ValidateCode.aspx?w=100&h=32改成:src='ValidateCode.aspx?w=128&h=40。
(2)“看不清楚”处还有一个参数dd,是用来做生成随机值以便更新验证码图片的。

作者:阿山Net







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.