如何进行网站验证码制作-创新互联

这篇文章给大家介绍如何进行网站验证码制作,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、虚拟空间、营销软件、网站建设、莎车网站维护、网站推广。

asp.net验证码制作


using System;

using System.IO;

using System.Drawing;

using System.Drawing.Imaging;

using System.Text;

using System.Collections;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

namespace WebApplication1

{

  public partial class yzm : System.Web.UI.Page

  {

    protected void Page_Load(object sender, EventArgs e)

{

string chkCode = string.Empty;

//颜色列表,用于验证码、噪线、噪点

Color[] color ={ Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };

//字体列表,用于验证码

string[] font ={ "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };

//验证码的字符集,去掉了一些容易混淆的字符

char[] character ={ '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };

Random rnd = new Random();

//生成验证码字符串

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

{

chkCode += character[rnd.Next(character.Length)];

}

//用session对象存储验证码

Session["name"] = chkCode;

Bitmap bmp = new Bitmap(100, 40);

Graphics g = Graphics.FromImage(bmp);

g.Clear(Color.White);

//画噪线

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

{

int x1 = rnd.Next(100);

int y1 = rnd.Next(40);

int x2 = rnd.Next(100);

int y2 = rnd.Next(40);

Color clr = color[rnd.Next(color.Length)];

g.DrawLine(new Pen(clr), x1, y1, x2, y2);

}

//画验证码字符串

for (int i = 0; i < chkCode.Length; i++)

{

string fnt = font[rnd.Next(font.Length)];

Font ft = new Font(fnt, 18);

Color clr = color[rnd.Next(color.Length)];

g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)8);

}

//画噪点

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

{

int x = rnd.Next(bmp.Width);

int y = rnd.Next(bmp.Height);

Color clr = color[rnd.Next(color.Length)];

bmp.SetPixel(x, y, clr);

}

//清除该页输出缓存,设置该页无缓存

Response.Buffer = true;

Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);

Response.Expires = 0;

Response.CacheControl = "no-cache";

Response.AppendHeader("Pragma", "No-Cache");

//将验证码图片写入内存流,并将其以 "p_w_picpath/Png" 格式输出

MemoryStream ms = new MemoryStream();

try

{

bmp.Save(ms, ImageFormat.Png);

Response.ClearContent();

Response.ContentType = "p_w_picpath/Png";

Response.BinaryWrite(ms.ToArray());

}

finally

{

//显式释放资源

bmp.Dispose();

g.Dispose();

}

}

  }

}

关于如何进行网站验证码制作就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

本文题目:如何进行网站验证码制作-创新互联
链接地址:https://www.cdcxhl.com/article12/cssjgc.html

成都网站建设公司_创新互联,为您提供微信小程序搜索引擎优化网站建设用户体验定制开发动态网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站制作