1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.IO;
 
public partial class Validate : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
      this.CreateCheckCodeImage(RndNum());
  }
  private string RndNum()
  {
      int number;
      char code;
      string checkCode = String.Empty;
      System.Random random = new Random();
      for (int i = 0; i < 4; i++)
      {
         number = random.Next();
         if (number % 2 == 0)
            code = (char)('0' + (char)(number % 10));
         else
            code = (char)('A' + (char)(number % 26));
         checkCode += code.ToString();
      }
      Session["CheckCode"] = checkCode;
      return checkCode;
  }
  private void CreateCheckCodeImage(string checkCode)
  {
      if (checkCode == null || checkCode.Trim() == String.Empty)
         return;
      System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
      Graphics g = Graphics.FromImage(image);
      try
      {
         // 生成随机生成器
         Random random = new Random();
         // 清空图片背景色
         g.Clear(Color.White);
         // 画图片的背景噪音线
         for (int i = 0; i < 25; 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);
      }
      Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
      System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image           .Height), Color.Blue, Color.DarkRed, 1.2f, true);
      g.DrawString(checkCode, font, brush, 2, 2);
         // 画图片的前景噪音点
         for (int i = 0; i < 100; i++)
         {
            int x = random.Next(image.Width);
            int y = random.Next(image.Height);
        image.SetPixel(x, y, Color.FromArgb(random.Next()));
      }
      //画图片的边框线
      g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
      System.IO.MemoryStream ms = new System.IO.MemoryStream();
      image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
      Response.ClearContent();
      Response.ContentType = "image/Gif";
      Response.BinaryWrite(ms.ToArray());
    }
    finally
    {
      g.Dispose();
      image.Dispose();
    }
  }
}

2. 在前台你想添加的页面添加这段代码:(我这里假设是在登录页面Login.aspx中添加图片验证码)

<img id="ImageCode" src="../Validate.aspx" style="cursor:pointer" onmouseup="RefreshImage()" alt="点击重刷新"/>

还需要在<head>...</head>之间添加这段代码:


<script language ="javascript" type="text/javascript" >
  function RefreshImage()
   {
    var img = document.getElementById("ImageCode"); // 这里的ImageCode就是上面你取图片的Id名字,这里要一致!
    img.src = img.src + '?'; 
  }
</script>

3. 在后台Login.aspx.cs中添加实现代码:


引用......
public partial class Login : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  }
  // 登录按钮点击事件处理
  protected void btnLogin_Click(object sender, ImageClickEventArgs e)
  {
    User user = new User();
    user.LoginId = this.txt_LoginId.Text.Trim();
    user.LoginPwd = this.txt_LoginPwd.Text.Trim();
    if (this.txtCode.Text.ToLower() == Session["CheckCode"].ToString().ToLower())
    {
      Response.Redirect("Index.aspx");
      else
      {
        Session["user"] = null;
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "str", "<script>alert(\"登录失败!!\")</script>");
      }
    }
    else
    {
      this.Page.ClientScript.RegisterStartupScript(this.GetType(), "str", "<script>alert(\"验证码错误!!\")</script>");
    }
  }
  // 新用户注册按钮点击事件处理
  protected void btn_Register_Click(object sender, ImageClickEventArgs e)
  {
    Response.Redirect("Register.aspx");
  }
}

ASP.NET图片验证码的更多相关文章

  1. ASP.NET图片验证码学习!

    1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...

  2. asp.net web api实现图片点击式图片验证码

    现在验证码的形式越来越丰富,今天要实现的是在点击图片中的文字来进行校验的验证码,如图 这种验证码验证是验证鼠标是否选中了图片中文字的位置,以及选择的顺序,产生验证码的时候可以提供一组底图,然后随机获取 ...

  3. ASP.NET中图片验证码与js获取验证码的值

    现在的程序中,为了防止用户恶意点击,我们一般都会加上验证,现在比较普遍的是加上图片验证码或者手机短信验证.验证码一般都是防机器不防人,有效的防止了恶意点击. 那么在webform中如何生成动态的图片验 ...

  4. ASP.NET MVC 模块与组件(二)——定制图片验证码

     本着简洁直接,我们就直奔主题吧! 下面是一个生成数字和字母随机组合的验证码类源代码: using System; using System.Drawing; using System.Drawing ...

  5. asp.net core 图片验证码,后台验证

    验证方法: public static string VerificationCodeCacheFormat="vcode_cache_{0}"; public IActionRe ...

  6. [Asp.Net Core] 为什么选择 Blazor Server Side (一) 快速实现图片验证码

    关于Blazor 由于在国内, Blazor一点都不普及, 建议读者翻看我之前写的随笔, 了解Blazor Server Side的特点. 在一段时间内, 我会写一些解说分析型的 "为什么选 ...

  7. webform(十)——图片水印和图片验证码

    两者都需要引入命名空间:using System.Drawing; 一.图片水印 前台Photoshuiyin.aspx代码: <div> <asp:FileUpload ID=&q ...

  8. Webform 文件上传、 C#加图片水印 、 图片验证码

    文件上传:要使用控件 - FileUpload 1.如何判断是否选中文件? FileUpload.FileName - 选中文件的文件名,如果长度不大于0,那么说明没选中任何文件 js - f.val ...

  9. ExtJS4图片验证码的实现

    ExtJS4学习笔记(十)---ExtJS4图片验证码的实现 转自:http://blog.sina.com.cn/s/blog_8d4bbd890100xaxh.html     上多少篇文章,重要 ...

随机推荐

  1. 转:阿里开源Mysql分布式中间件:Cobar

    原文来自于:http://hualong.iteye.com/blog/2102798 这几天研究了下Cobar, Cobar是阿里巴巴研发的关系型数据的分布式处理系统(Amoeba的升级版,该产品成 ...

  2. Python3.x和Python2.x的区别-转

    这个星期开始学习Python了,因为看的书都是基于Python2.x,而且我安装的是Python3.1,所以书上写的地方好多都不适用于Python3.1,特意在Google上search了一下3.x和 ...

  3. Batik - 将svg转换成其他格式图片或PDF - [导出服务器配置] 导出服务器原理解析

    导出服务器原理解析 Highcharts图表导出(或下载)本质上是将SVG代码转换为不同文件格式的过程,用到的工具是batik,所以所谓导出服务器,只不过是调用batik,将SVG代码转换并下载.下图 ...

  4. [cocos2d] 谁摸了我一下----触摸事件处理

    1. 设置接受触摸事件,可在init方法里面写上 [self setTouchEnabled: YES]; 旧版为self.isTouchEnabled = YES; xcode会报Deprecati ...

  5. 【HDOJ】1811 Rank of Tetris

    并查集+拓扑排序.使用并查集解决a = b的情况. #include <iostream> #include <cstdio> #include <cstring> ...

  6. BZOJ2342: [Shoi2011]双倍回文

    2342: [Shoi2011]双倍回文 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 923  Solved: 317[Submit][Status ...

  7. 1047 - Neighbor House(简单线性DP)

    题目大意: 给你n个房子,要求把房子染成R,G,B三种的一种颜色, 要求相邻的颜色不能一样. dp[第i个房子][第j种颜色]  转移一下就行了.       #include<cstdio&g ...

  8. 8个超炫酷仿HTML5动画源码

    1.jQuery万年历插件 带农历老皇历功能 这是一款基于jQuery的日历插件,这款日历插件和之前分享的日历控件有很大差异,它是一本万年历,包含了农历已经老皇历的功能,是一个挑好日子的工具.同时日历 ...

  9. Move Zeroes——Leetcode

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  10. UnderStand Perspective Rasterization, SV_POSITION(gl_FragCoord) to Pixel, SV mean Systems Value

    Shader "UnderStandPRR" { Properties { _MainTex ("Texture", 2D) = "white&quo ...