国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.net 驗證碼(C#)

2024-07-10 13:07:21
字體:
來源:轉載
供稿:網友
public class validatecode : system.web.ui.page
   {
   private void page_load(object sender, system.eventargs e)
   {
   this.createcheckcodeimage(generatecheckcode());
   }
  
   #region web 窗體設計器生成的代碼
   override protected void oninit(eventargs e)
   {
   //
   // codegen: 該調用是 asp.net web 窗體設計器所必需的。
   //
   initializecomponent();
   base.oninit(e);
   }
  
   /// <summary>
   /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
   /// 此方法的內容。
   /// </summary>
   private void initializecomponent()
   {
   this.load += new system.eventhandler(this.page_load);
   }
   #endregion
  
   private string generatecheckcode()
   {
   int number;
   char code;
   string checkcode = string.empty;
  
   system.random random = new random();
  
   for(int i=0; i<5; i++)
   {
   number = random.next();
  
   if(number % 2 == 0)
   code = (char)('0' + (char)(number % 10));
   else
   code = (char)('a' + (char)(number % 26));
  
   checkcode += code.tostring();
   }
  
   response.cookies.add(new httpcookie("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();
   }
   }
   }


收集最實用的網頁特效代碼!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 封丘县| 开化县| 鹰潭市| 宁陵县| 甘孜县| 静海县| 苍溪县| 南和县| 平原县| 大荔县| 页游| 上虞市| 绩溪县| 涪陵区| 潞西市| 中宁县| 万荣县| 隆林| 边坝县| 萍乡市| 日土县| 厦门市| 加查县| 定南县| 台山市| 溧水县| 宜丰县| 黄梅县| 雷波县| 广河县| 疏附县| 安平县| 普兰店市| 万州区| 诸城市| 郯城县| 台江县| 佛冈县| 乐业县| 府谷县| 余庆县|