欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C#實(shí)現(xiàn)的一款比較美觀的驗(yàn)證碼完整實(shí)例

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

本文實(shí)例講述了C#實(shí)現(xiàn)的一款比較美觀的驗(yàn)證碼。分享給大家供大家參考,具體如下:

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;//單個(gè)字體的寬度范圍
 private int letterHeight = 22;//單個(gè)字體的高度范圍
 private int letterCount = 4;//驗(yàn)證碼位數(shù)
 private char[] chars = "0123456789".ToCharArray();
 private string[] fonts = { "Arial", "Georgia" };
 /// <summary>
 /// 產(chǎn)生波形濾鏡效果
 /// </summary>
 private const double PI = 3.1415926535897932384626433832795;
 private const double PI2 = 6.283185307179586476925286766559;
 protected void Page_Load(object sender, EventArgs e)
 {
  //防止網(wǎng)頁(yè)后退--禁止緩存
  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);
  //生成隨機(jī)生成器
  Random random = new Random();
  //白色背景
  g.Clear(Color.White);
  //畫(huà)圖片的背景噪音線
  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);
  }
  //畫(huà)圖片的前景噪音點(diǎn)
  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()));
  }
  //隨機(jī)字體和顏色的驗(yàn)證碼字符
  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);
  //將生成的圖片發(fā)回客戶端
  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">如果扭曲則選擇為T(mén)rue</param>
 /// <param name="nMultValue">波形的幅度倍數(shù),越大扭曲的程度越高,一般為3</param>
 /// <param name="dPhase">波形的起始相位,取值區(qū)間[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);
    // 取得當(dāng)前點(diǎn)的顏色
    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
 }
 // 生成隨機(jī)數(shù)字字符串
 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;
 }
}

復(fù)制代碼 代碼如下:

<img alt="看不清,換一張" title="看不清,換一張" src="ValidateCode.aspx" style="cursor:pointer" onclick="this.src=this.src+'?r='+Math.random()" />

更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《C#中XML文件操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。

上一篇:C# 7.0 新特性1之基于Tuple的“多”返回值方法

欄    目:C#教程

下一篇:C#.net編程創(chuàng)建Access文件和Excel文件的方法詳解

本文標(biāo)題:C#實(shí)現(xiàn)的一款比較美觀的驗(yàn)證碼完整實(shí)例

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6438.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有