- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>无标题页</title>
- <script language="javascript">
- //验证码:读取cookie中的值
- function readCookie()
- {
- //判断是否存在cookie
- var isExist = document.cookie.indexOf("imgRandom=");
- //alert(isExist);
- if(isExist!=-1)
- {
- //“imgRandom=”后面的等号的位置
- var c= document.cookie.indexOf("=",isExist)+1;
- //获取等号后面的字符串,也就是所需要的验证码!
- var str = document.cookie.substring(c)
- //alert(document.cookie.substring(c));
- if(document.getElementById("Image").value!=str)
- {
- document.getElementById("Image").select();
- alert("验证码输入错误!")
- return false;
- }else
- {
- return true;
- }
- }
- }
- //onload = readCookie;
- //获取图片的src
- function imgSrc()
- {
- // 通过链接让图片中的src发生变化
- document.getElementById("imgRandom").src = document.getElementById("imgRandom").src + "?abc=" + Math.random();
- }
- </script>
- </head>
- <body>
- <form name="vcode" action="Default.aspx" method="post" onsubmit=" readCookie()">
- 验证码 : <input id="Image" type="text" name="checkImage" style="width: 72px; height:20px" /><span style="vertical-align:bottom"><img id="imgRandom" src="Randomimage.aspx" alt="" /></span><a href="javascript:imgSrc()">看不清换一张</a><br />
- <input type="submit" value="确定" style="border:solid 1px black" />
- </form>
- </body>
- </html>
- using System;
- using System.Data;
- using System.Configuration;
- 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;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- }
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Randomimage.aspx.cs" Inherits="Randomimage" %>
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Drawing;
- using System.IO;
- public partial class Randomimage :System.Web.UI.Page
- {
- public void Page_Load(object sender, EventArgs e)
- {
- string random = this.random();
- /*
- * 将随机数字添加到cookie中
- */
- //添加cookie
- //imgCookie.Value = random;
- //this.Response.Cookies.Add(imgCookie);
- //this.Response.Write(imgCookie.Value);
- /*
- * 图片加载的时候,在图片上写随机数
- */
- int width = Convert.ToInt32(random.Length * 25);
- using (Bitmap image = new Bitmap(width, 40))
- {
- //应用画板,在画板上书写数字
- using (Graphics gp = Graphics.FromImage(image))
- {
- //用字符串的形式书写数字
- //gp.DrawString(random, new Font("宋体", 24), Brushes.Red, new PointF(20.0F, 10.0F));
- gp.Clear(Color.White);
- Font font = new Font("隶书", 24, FontStyle.Bold);
- System.Drawing.Drawing2D.LinearGradientBrush brush =
- new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.DarkOrchid, Color.Blue, 3.5f, true);
- gp.DrawString(random, font, brush, 3, 2);
- this.drawLine(gp, image);
- }
- //显示图片
- //image.Save(this.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
- MemoryStream ms = new MemoryStream();
- image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
- //输出图片
- Response.ClearContent();
- Response.ContentType = "text/html";
- Response.BinaryWrite(ms.ToArray());
- }
- HttpCookie imgCookie = null;
- if (this.Request.Cookies["imgRandom"] == null)
- {
- imgCookie = new HttpCookie("imgRandom");
- imgCookie.Value = random;
- this.Response.Cookies.Add(imgCookie);
- //this.Response.Redirect("image.aspx");
- }
- else
- {
- imgCookie = new HttpCookie("imgRandom");
- imgCookie.Value = random;
- this.Response.Cookies.Add(imgCookie);
- }
- }
- /*
- * 随机数字的获取
- */
- public string random()
- {
- //string number = "";
- //Random random = new Random();
- //for (int i = 0; i < 4; i++)
- //{
- // number += (random.Next(0, 10)).ToString();
- //}
- string guid = Guid.NewGuid().ToString();
- string rtuStr = Guid.NewGuid().ToString().Substring(1, 4);
- return rtuStr;
- }
- private void drawPoint(Bitmap img)
- {
- Random ran = new Random();
- 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));
- }
- }
- private void drawLine(Graphics gfc, Bitmap img)
- {
- Random ran = new Random();
- //选择画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); //注意画笔一定要浅颜色,否则验证码看不清楚
- }
- }
- }
本文来源:
我的异常网
Java Exception
Dotnet Exception
Oracle Exception
- 2496 - System.Data.SqlClient.SqlException.Number
- 2497 - ORA-01436: CONNECT BY loop in user data
- 2498 - ORA-00907: 缺失右括号
- 2499 - 存储过程错误
- 2500 - 为什么总是提示缺少关键字
- 2501 - oracle的多表分页存储过程调用出错
- 2502 - ORA-00604:递归SQL级别3出现错误
- 2503 - ORA-04031:无法分配4096字节的共享内存
- 2504 - 怎样用一条语句判断用户名错误还是密码错误
- 2505 - ADO连接ORCALE出现连接错误
- 2506 - 索引超出范围
- 2507 - update错误
- 2508 - ORA-00984: column not allowed here
- 2509 - 无效数字
- 2510 - ORA-12560:TNS:协议适配器错误
- 2511 - Oracle 10g宝典
- 2512 - ORA-01843: not a valid month
- 2513 - 名称已由现有对象使用
- 2514 - oracle 查看系统定义异常的sql语句
- 2515 - Unable to read the list of homes from the inventory