先要新建validate aspx验证页面。然后生成验证码控件p_w_picpathbutton,再生成picture窗体。
公司主营业务:成都网站设计、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出东城免费做网站回馈大家。代码如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Drawing;
using System.IO;
public partial class Picture : System.Web.UI.Page
{
Random ran = new Random();
protected void Page_Load(object sender, EventArgs e)
{
string str = getRandomValidate(4);
Session["CheckCode"] = str;//这一部是Wie了验证码写入Session,进行验证,也可以使用cookie
getImageValidate(str);
}
//得到随机字符串,长度自定义
private string getRandomValidate(int len)
{
int num;
int tem;
string rtuStr="";
for (int i = 0; i < len;i++ )
{
num = ran.Next();
tem = num % 10 + '0';//生成数字
//tem = num % 26 + 'A';//生成字符
rtuStr += Convert.ToChar(tem).ToString();public partial class Picture : System.Web.UI.Page
{
Random ran = new Random();
protected void Page_Load(object sender, EventArgs e)
{
string str = getRandomValidate(4);
Session["CheckCode"] = str;//这一部是Wie了验证码写入Session,进行验证,也可以使用cookie
getImageValidate(str);
}
//得到随机字符串,长度自定义
private string getRandomValidate(int len)
{
int num;
int tem;
string rtuStr="";
for (int i = 0; i < len;i++ )
{
num = ran.Next();
tem = num % 10 + '0';//生成数字
//tem = num % 26 + 'A';//生成字符
rtuStr += Convert.ToChar(tem).ToString();public partial class Picture : System.Web.UI.Page
{
Random ran = new Random();
protected void Page_Load(object sender, EventArgs e)
{
string str = getRandomValidate(4);
Session["CheckCode"] = str;//这一部是Wie了验证码写入Session,进行验证,也可以使用cookie
getImageValidate(str);
}
//得到随机字符串,长度自定义
private string getRandomValidate(int len)
{
int num;
int tem;
string rtuStr="";
for (int i = 0; i < len;i++ )
{
num = ran.Next();
tem = num % 10 + '0';//生成数字
//tem = num % 26 + 'A';//生成字符
rtuStr += Convert.ToChar(tem).ToString();
}
return rtuStr;
}
//生成图像
private void getImageValidate(string strValue)
{
//string str=oo00;前两个为字母o,后两个数为0
int width = Convert.ToInt32(strValue.Length*12);
Bitmap img = new Bitmap(width,23);
Graphics gfc = Graphics.FromImage(img);
gfc.Clear(Color.White);
drawLine(gfc,img);
//写验证码,要定义Font
Font font = new Font("arial",12,FontStyle.Bold);
//Font font = new Font("宋体",12,FontStyle.Bold|FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush brush = new
System.Drawing.Drawing2D.LinearGradientBrush(new
Rectangle(0,0,img.Width,img.Height),Color.DarkOrchid,Color.Blue,1.5f,true);
gfc.DrawString(strValue,font, brush ,3,2);
drawPoint(img);
gfc.DrawRectangle(new Pen(Color.DarkBlue),0,0,img.Width-1,img.Height-1);
//将图像添加到页面
MemoryStream ms = new MemoryStream();
img.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
//更改HTTP
Response.ClearContent();
Response.ContentType = "p_w_picpath/gif";
Response.BinaryWrite(ms.ToArray());
//Dispose
gfc.Dispose();
img.Dispose();
Response.End();
}
private void drawLine(Graphics gfc,Bitmap img)
{
//选择画10条线,也可以增加,也可以不要线,只要随机杂点就行
for (int i = 0; i < 10;i++ )
{
int x1 = ran.Next(img.Width);
int y1 = ran.Next(img.Height);
int x2 = ran.Next(img.Width);
int y2 = ran.Next(img.Height);
gfc.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);//注意画笔要淡,不然看不清
}
}
//private void drawPoint(Bitmap img)
//{
//}
private void drawPoint(Bitmap img)
{
int col = ran.Next();//在一次的图片中杂点颜色相同
for (int i = 0; i < 100; i++)
{
int x = ran.Next(img.Width);
int y = ran.Next(img.Height);
img.SetPixel(x,y,Color.FromArgb(col));
}
}
}
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
当前标题:asp.net如何实现生成验证码的登录界面-创新互联
本文URL:http://lswzjz.com/article/dijepg.html