C#实现的一款比较美观的验证码完整实例
程序员文章站
2022-05-28 19:24:51
本文实例讲述了c#实现的一款比较美观的验证码。分享给大家供大家参考,具体如下:
using system;
using system.collections.g...
本文实例讲述了c#实现的一款比较美观的验证码。分享给大家供大家参考,具体如下:
using system; using system.collections.generic; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.drawing; using system.io; using system.drawing.imaging; public partial class validatecode : system.web.ui.page { private int letterwidth = 16;//单个字体的宽度范围 private int letterheight = 22;//单个字体的高度范围 private int lettercount = 4;//验证码位数 private char[] chars = "0123456789".tochararray(); private string[] fonts = { "arial", "georgia" }; /// <summary> /// 产生波形滤镜效果 /// </summary> private const double pi = 3.1415926535897932384626433832795; private const double pi2 = 6.283185307179586476925286766559; protected void page_load(object sender, eventargs e) { //防止网页后退--禁止缓存 response.expires = 0; response.buffer = true; response.expiresabsolute = datetime.now.addseconds(-1); response.addheader("pragma", "no-cache"); response.cachecontrol = "no-cache"; string str_validatecode = getrandomnumberstring(lettercount); httpcookie objcookie = new httpcookie("validatecode"); objcookie.value = str_validatecode; objcookie.path = "/"; objcookie.expires = datetime.now.addseconds(1200); response.cookies.add(objcookie); createimage(str_validatecode); } public void createimage(string checkcode) { int int_imagewidth = checkcode.length * letterwidth; random newrandom = new random(); bitmap image = new bitmap(int_imagewidth, letterheight); graphics g = graphics.fromimage(image); //生成随机生成器 random random = new random(); //白色背景 g.clear(color.white); //画图片的背景噪音线 for (int i = 0; i < 10; i++) { int x1 = random.next(image.width); int x2 = random.next(image.width); int y1 = random.next(image.height); int y2 = random.next(image.height); g.drawline(new pen(color.silver), x1, y1, x2, y2); } //画图片的前景噪音点 for (int i = 0; i < 10; i++) { int x = random.next(image.width); int y = random.next(image.height); image.setpixel(x, y, color.fromargb(random.next())); } //随机字体和颜色的验证码字符 int findex; for (int int_index = 0; int_index < checkcode.length; int_index++) { findex = newrandom.next(fonts.length - 1); string str_char = checkcode.substring(int_index, 1); brush newbrush = new solidbrush(getrandomcolor()); point thepos = new point(int_index * letterwidth + 1 + newrandom.next(3), 1 + newrandom.next(3));//5+1+a+s+p+x g.drawstring(str_char, new font(fonts[findex], 12, fontstyle.bold), newbrush, thepos); } //灰色边框 g.drawrectangle(new pen(color.lightgray, 1), 0, 0, int_imagewidth - 1, (letterheight - 1)); //图片扭曲 //image = twistimage(image, true, 3, 4); //将生成的图片发回客户端 memorystream ms = new memorystream(); image.save(ms, imageformat.png); response.clearcontent(); //需要输出图象信息 要修改http头 response.contenttype = "image/png"; response.binarywrite(ms.toarray()); g.dispose(); image.dispose(); } /// <summary> /// 正弦曲线wave扭曲图片 /// </summary> /// <param name="srcbmp">图片路径</param> /// <param name="bxdir">如果扭曲则选择为true</param> /// <param name="nmultvalue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param> /// <param name="dphase">波形的起始相位,取值区间[0-2*pi)</param> /// <returns></returns> public system.drawing.bitmap twistimage(bitmap srcbmp, bool bxdir, double dmultvalue, double dphase) { system.drawing.bitmap destbmp = new bitmap(srcbmp.width, srcbmp.height); // 将位图背景填充为白色 system.drawing.graphics graph = system.drawing.graphics.fromimage(destbmp); graph.fillrectangle(new solidbrush(system.drawing.color.white), 0, 0, destbmp.width, destbmp.height); graph.dispose(); double dbaseaxislen = bxdir ? (double)destbmp.height : (double)destbmp.width; for (int i = 0; i < destbmp.width; i++) { for (int j = 0; j < destbmp.height; j++) { double dx = 0; dx = bxdir ? (pi2 * (double)j) / dbaseaxislen : (pi2 * (double)i) / dbaseaxislen; dx += dphase; double dy = math.sin(dx); // 取得当前点的颜色 int noldx = 0, noldy = 0; noldx = bxdir ? i + (int)(dy * dmultvalue) : i; noldy = bxdir ? j : j + (int)(dy * dmultvalue); system.drawing.color color = srcbmp.getpixel(i, j); if (noldx >= 0 && noldx < destbmp.width && noldy >= 0 && noldy < destbmp.height) { destbmp.setpixel(noldx, noldy, color); } } } return destbmp; } 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(210); int int_green = randomnum_sencond.next(180); int int_blue = (int_red + int_green > 300) ? 0 : 400 - int_red - int_green; int_blue = (int_blue > 255) ? 255 : int_blue; return color.fromargb(int_red, int_green, int_blue);// 5+1+a+s+p+x } // 生成随机数字字符串 public string getrandomnumberstring(int int_numberlength) { random random = new random(); string validatecode = string.empty; for (int i = 0; i < int_numberlength; i++) validatecode += chars[random.next(0, chars.length)].tostring(); return validatecode; } }
复制代码 代码如下:
<img alt="看不清,换一张" title="看不清,换一张" src="validatecode.aspx" style="cursor:pointer" onclick="this.src=this.src+'?r='+math.random()" />
更多关于c#相关内容感兴趣的读者可查看本站专题:《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结》
希望本文所述对大家c#程序设计有所帮助。
上一篇: 我们不一样啊
下一篇: C#判断字符编码的方法总结(六种方法)