免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

ASP.NET中怎么實現驗證碼-創新互聯

ASP.NET中怎么實現驗證碼,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

成都創新互聯,為您提供網站建設公司成都網站制作、網站營銷推廣、網站開發設計,對服務玻璃鋼坐凳等多個行業擁有豐富的網站建設及推廣經驗。成都創新互聯網站建設公司成立于2013年,提供專業網站制作報價服務,我們深知市場的競爭激烈,認真對待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發展進步,是我們永遠的責任!

代碼主要就是繪制驗證碼類的實現


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;

namespace SecurityCodePic
{
 public class DrawingSecurityCode
 {
  /// <summary>
  /// 生成驗證碼,并返回
  /// </summary>
  /// <returns></returns>
  public string GetSecurityCode(int n)
  {
   string code = GenerateCheckCode(n);
   CreateCheckCodeImage(code);
   return code;
  }

  /// <summary>
  /// 動態生成指定數目的隨機數或字母
  /// </summary>
  /// <param name="num">整數</param>
  /// <returns>返回驗證碼字符串</returns>
  private string GenerateCheckCode(int num)
  {
   int number;//定義變量
   char code;
   string checkCode = String.Empty; //空字符串,只讀
   Random random = new Random(); //定義隨機變量實例
   for (int i=0; i < num;i++ )
   {
    //利用for循環生成指定數目的隨機數或字母
    number = random.Next(); //返回一個小于指定的較大值的非負的隨機數 next有三個構造函數 
    if (number % 2 == 0)
    {//產生一個一位數
     code = (char)('0' + (char)(number % 10));
    }
    else
    { //產生一個大寫字母
     code = (char)('A'+(char)(number % 26));
    }
    checkCode += code.ToString();
   }
   return checkCode;
  }

  /// <summary>
  /// 根據驗證碼字符串生成驗證碼圖片
  /// </summary>
  /// <param name="checkCode">驗證碼字符串</param>
  private void CreateCheckCodeImage(string checkCode)
  {

   if (checkCode == null || checkCode.Trim() == String.Empty) return;
   // 引用System.Drawing類庫
   Bitmap myImage = new Bitmap(80, 30);//生成一個指定大小的位圖
   Graphics graphics = Graphics.FromImage(myImage); //從一個位圖生成一個畫布
   try
   {
    graphics.Clear(Color.White); //清除整個繪畫面并以指定的背景色填充,這里是把背景色設為白色
    Random random = new Random(); //實例化一個偽隨機數生成器

    //畫圖片的前景噪音點,這里有100個
    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()));//指定坐標為x,y處的像素的顏色
    }

    //畫圖片的背景噪音線,這里為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); //繪制一條坐標x1,y1到坐標x2,y2的指定顏色的線條,這里的線條為黑色
    }

    Font font = new Font("Arial", 15, FontStyle.Bold); //定義特定的文本格式,這里的字體為Arial,大小為15,字體加粗
    //根據矩形、起始顏色和結束顏色以及方向角度產生一個LinearGradientBrush實例---線性漸變
    System.Drawing.Drawing2D.LinearGradientBrush brush =
     new System.Drawing.Drawing2D.LinearGradientBrush(
      new Rectangle(0, 0, myImage.Width, myImage.Height),//在坐標0,0處實例化一個和myImage同樣大小的矩形
      Color.Blue, Color.Red, 1.2f, true);
    //繪制文本字符串
    graphics.DrawString(checkCode, font, brush, 2, 2);

    //繪制有坐標對、寬度和高度指定的矩形---畫圖片的邊框線
    graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, myImage.Width - 1, myImage.Height - 1);
    //創建其支持存儲器為內存的流
    MemoryStream ms = new MemoryStream();
    //將此圖像以指定格式保存到指定的流中
    myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); //這里是以gif的格式保存到內存中
    HttpContext.Current.Response.ClearContent(); //清除緩沖區流中的所有內容輸出
    HttpContext.Current.Response.ContentType = "image/Gif"; //獲取或設置輸出流的HTTP MIME類型
    HttpContext.Current.Response.BinaryWrite(ms.ToArray()); //將一個二進制字符串寫入HTTP輸出流
   }
   finally
   {
    //釋放占用資源
    graphics.Dispose();
    myImage.Dispose();
   }
  }
 }
}


然后使用SecurityCode.ashx文件調用上面類的方法實現


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SecurityCodePic
{
 /// <summary>
 /// SecurityCode1 的摘要說明
 /// </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頁面圖片路徑的引用了


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SecurityCode_Test.aspx.cs" Inherits="SecurityCodePic.SecurityCode_Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/tupian/20230522/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>驗證碼的實現</title>
 <style type="text/css">
 #VCodeImg { cursor: pointer;}
 </style>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <img id="VCodeImg" src="SecurityCode.ashx" alt="驗證碼" 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; //加上無意義的隨機參數,瀏覽器才會認為是新地址,就會重新讀取數據
  }
 </script>
</body>
</html>

關于ASP.NET中怎么實現驗證碼問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創新互聯行業資訊頻道了解更多相關知識。

名稱欄目:ASP.NET中怎么實現驗證碼-創新互聯
URL網址:http://newbst.com/article34/dggspe.html

成都網站建設公司_創新互聯,為您提供App開發云服務器網頁設計公司關鍵詞優化網站收錄微信小程序

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

外貿網站建設