一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - ASP.NET教程 - ASP.NET驗(yàn)證碼實(shí)現(xiàn)(附源碼)

ASP.NET驗(yàn)證碼實(shí)現(xiàn)(附源碼)

2019-12-27 12:34Yluka ASP.NET教程

這篇文章主要介紹了ASP.NET驗(yàn)證碼實(shí)現(xiàn)過(guò)程,并為大家分享了源碼下載,感興趣的小伙伴們可以參考一下

首先看下效果實(shí)現(xiàn)(由于gif屏幕錄制軟件是即時(shí)找的,有些失禎)

ASP.NET驗(yàn)證碼實(shí)現(xiàn)(附源碼)

代碼主要就是繪制驗(yàn)證碼類的實(shí)現(xiàn)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
 
namespace SecurityCodePic
{
 public class DrawingSecurityCode
 {
  /// <summary>
  /// 生成驗(yàn)證碼,并返回
  /// </summary>
  /// <returns></returns>
  public string GetSecurityCode(int n)
  {
   string code = GenerateCheckCode(n);
   CreateCheckCodeImage(code);
   return code;
  }
 
  /// <summary>
  /// 動(dòng)態(tài)生成指定數(shù)目的隨機(jī)數(shù)或字母
  /// </summary>
  /// <param name="num">整數(shù)</param>
  /// <returns>返回驗(yàn)證碼字符串</returns>
  private string GenerateCheckCode(int num)
  {
   int number;//定義變量
   char code;
   string checkCode = String.Empty; //空字符串,只讀
   Random random = new Random(); //定義隨機(jī)變量實(shí)例
   for (int i=0; i < num;i++ )
   {
    //利用for循環(huán)生成指定數(shù)目的隨機(jī)數(shù)或字母
    number = random.Next(); //返回一個(gè)小于指定的最大值的非負(fù)的隨機(jī)數(shù) next有三個(gè)構(gòu)造函數(shù)
    if (number % 2 == 0)
    {//產(chǎn)生一個(gè)一位數(shù)
     code = (char)('0' + (char)(number % 10));
    }
    else
    { //產(chǎn)生一個(gè)大寫(xiě)字母
     code = (char)('A'+(char)(number % 26));
    }
    checkCode += code.ToString();
   }
   return checkCode;
  }
 
  /// <summary>
  /// 根據(jù)驗(yàn)證碼字符串生成驗(yàn)證碼圖片
  /// </summary>
  /// <param name="checkCode">驗(yàn)證碼字符串</param>
  private void CreateCheckCodeImage(string checkCode)
  {
 
   if (checkCode == null || checkCode.Trim() == String.Empty) return;
   // 引用System.Drawing類庫(kù)
   Bitmap myImage = new Bitmap(80, 30);//生成一個(gè)指定大小的位圖
   Graphics graphics = Graphics.FromImage(myImage); //從一個(gè)位圖生成一個(gè)畫(huà)布
   try
   {
    graphics.Clear(Color.White); //清除整個(gè)繪畫(huà)面并以指定的背景色填充,這里是把背景色設(shè)為白色
    Random random = new Random(); //實(shí)例化一個(gè)偽隨機(jī)數(shù)生成器
 
    //畫(huà)圖片的前景噪音點(diǎn),這里有100個(gè)
    for (int i = 0; i < 100; i++)
    {
     int x = random.Next(myImage.Width);
     int y = random.Next(myImage.Height);
     myImage.SetPixel(x, y, Color.FromArgb(random.Next()));//指定坐標(biāo)為x,y處的像素的顏色
    }
 
    //畫(huà)圖片的背景噪音線,這里為2條
    for (int i = 0; i < 2; i++)
    {
     int x1 = random.Next(myImage.Width);
     int x2 = random.Next(myImage.Width);
     int y1 = random.Next(myImage.Height);
     int y2 = random.Next(myImage.Height);
     graphics.DrawLine(new Pen(Color.Black), x1, y1, x2, y2); //繪制一條坐標(biāo)x1,y1到坐標(biāo)x2,y2的指定顏色的線條,這里的線條為黑色
    }
 
    Font font = new Font("Arial", 15, FontStyle.Bold); //定義特定的文本格式,這里的字體為Arial,大小為15,字體加粗
    //根據(jù)矩形、起始顏色和結(jié)束顏色以及方向角度產(chǎn)生一個(gè)LinearGradientBrush實(shí)例---線性漸變
    System.Drawing.Drawing2D.LinearGradientBrush brush =
     new System.Drawing.Drawing2D.LinearGradientBrush(
      new Rectangle(0, 0, myImage.Width, myImage.Height),//在坐標(biāo)0,0處實(shí)例化一個(gè)和myImage同樣大小的矩形
      Color.Blue, Color.Red, 1.2f, true);
    //繪制文本字符串
    graphics.DrawString(checkCode, font, brush, 2, 2);
 
    //繪制有坐標(biāo)對(duì)、寬度和高度指定的矩形---畫(huà)圖片的邊框線
    graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, myImage.Width - 1, myImage.Height - 1);
    //創(chuàng)建其支持存儲(chǔ)器為內(nèi)存的流
    MemoryStream ms = new MemoryStream();
    //將此圖像以指定格式保存到指定的流中
    myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); //這里是以gif的格式保存到內(nèi)存中
    HttpContext.Current.Response.ClearContent(); //清除緩沖區(qū)流中的所有內(nèi)容輸出
    HttpContext.Current.Response.ContentType = "image/Gif"; //獲取或設(shè)置輸出流的HTTP MIME類型
    HttpContext.Current.Response.BinaryWrite(ms.ToArray()); //將一個(gè)二進(jìn)制字符串寫(xiě)入HTTP輸出流
   }
   finally
   {
    //釋放占用資源
    graphics.Dispose();
    myImage.Dispose();
   }
  }
 }
}


然后使用SecurityCode.ashx文件調(diào)用上面類的方法實(shí)現(xiàn)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace SecurityCodePic
{
 /// <summary>
 /// SecurityCode1 的摘要說(shuō)明
 /// </summary>
 public class SecurityCode1 : IHttpHandler
 {
 
  public void ProcessRequest(HttpContext context)
  {
   DrawingSecurityCode sc = new DrawingSecurityCode();
   string SecurityCode = sc.GetSecurityCode(6);
  }
 
  public bool IsReusable
  {
   get
   {
    return false;
   }
  }
 }
}


最后就是ASP.NET頁(yè)面圖片路徑的引用了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SecurityCode_Test.aspx.cs" Inherits="SecurityCodePic.SecurityCode_Test" %>
 
<!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>驗(yàn)證碼的實(shí)現(xiàn)</title>
 <style type="text/css">
 #VCodeImg { cursor: pointer;}
 </style>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <img id="VCodeImg" src="SecurityCode.ashx" alt="驗(yàn)證碼" onclick="javascript:RefreshCode();" />
 </div>
 </form>
 <script type="text/javascript">
  function RefreshCode() {
   var random = Math.random();
   var img = document.getElementById("VCodeImg");
   img.src = "SecurityCode.ashx?" + random; //加上無(wú)意義的隨機(jī)參數(shù),瀏覽器才會(huì)認(rèn)為是新地址,就會(huì)重新讀取數(shù)據(jù)
  }
 </script>
</body>
</html>

以上就是本文的全部,對(duì)了,還有源碼下載分享給大家,歡迎大家下載。

源碼分享:ASP.NET驗(yàn)證碼實(shí)現(xiàn)

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲国产欧美目韩成人综合 | 国产精品亚洲片夜色在线 | 欧美日韩久久中文字幕 | 国产悠悠视频在线播放 | 公妇仑乱在线观看 | 男男双性生子产乳高辣h | 国产精品香蕉在线观看不卡 | 日本黄a三级三级三级 | 校草太大了h| 日本道色综合久久影院 | 日韩一品在线播放视频一品免费 | 国产特级 | 91在线精品国产丝袜超清 | 久久日韩精品无码一区 | 精品国产原创在线观看视频 | 99ri在线视频网 | 欧美成人另类人妖 | 欧美日韩国产一区二区三区在线观看 | 国产婷婷高清在线观看免费 | 日韩精品一区二区三区中文版 | 亚洲欧美色综合图小说 | 娇妻与老头绿文小说系列 | 欧洲喷浆乌克兰 | 四虎1515hhcom| bb18lv黑料正能量 | hd在线观看免费高清视频 | 夫妻性生活一级黄色片 | 国产福利一区二区三区 | 精品国产乱码久久久久久人妻 | 色戒完整版 | www四虎| 精品区卡一卡2卡三免费 | 日韩一二三 | sxx免费看观看美女 sss亚洲国产欧美一区二区 | 免费视频亚洲 | 水野朝阳厨房系列在线观看 | 美女吃jj| 好紧好爽的午夜寂寞视频 | 美女露鸡鸡 | 国产精品成人网红女主播 | 欧美bbb人妖 |