zoukankan      html  css  js  c++  java
  • NET 很简单的一个生成随机验证码封装的一个类

    1. using System;
    2. using System.Data;
    3. using System.Configuration;
    4. using System.Web;
    5. using System.Web.Security;
    6. using System.Web.UI;
    7. using System.Web.UI.WebControls;
    8. using System.Web.UI.WebControls.WebParts;
    9. using System.Web.UI.HtmlControls;
    10. using System.Drawing;
    11. /// <summary>
    12. /// VerifyCode 的摘要说明 产生随机数
    13. /// </summary>
    14. public class VerifyCode
    15. {
    16.     public VerifyCode()
    17.     {
    18.     }
    19.     //产生随机字符串
    20.     /// <summary>
    21.     /// 产生随机数
    22.     /// </summary>
    23.     /// <param name="num">需要的字符的个数</param>
    24.     /// <returns>返回产生的随机数</returns>
    25.     public string GenCode(int num)
    26.     {
    27.         //定义一个数组
    28.         string[] source ={"0","1","2","3","4","5","6","7","8","9",
    29.                             "A","B","C","D","E","F","G","H","I","J","K","L","M","N",
    30.                             "O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    31.         //定义一个装数组的字符串
    32.         string code = "";
    33.         Random rd = new Random();
    34.         for (int i = 0; i < num; i++)
    35.         {
    36.             code += source[rd.Next(0, source.Length)]; 
    37.         }
    38.         return code;//返回产生的随机数
    39.     }
    40.     //生成图片
    41.     public void GenImg(string code, Page curPage)
    42.     {
    43.         Bitmap myPalette = new Bitmap(60, 20);//定义一个画板
    44.         Graphics gh = Graphics.FromImage(myPalette);//在画板上定义绘图的实例
    45.         Rectangle rc = new Rectangle(0, 0, 60, 20);//定义一个矩形
    46.         gh.FillRectangle(new SolidBrush(Color.Blue), rc);//填充矩形
    47.         gh.DrawString(code, new Font("宋体", 16), new SolidBrush(Color.White), rc);//在矩形内画出字符串
    48.         myPalette.Save(curPage.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图片显示出来
    49.         curPage.Session["ValidateCode"] = code;//将字符串保存到Session中,以便需要时进行验证
    50.         gh.Dispose();//关闭 
    51.         myPalette.Dispose();
    52.     }
    53. }
  • 相关阅读:
    数组与指针
    壁纸
    2019/1/11
    指针A
    数组B
    一起来抓老鼠啊!快乐呀!
    打印沙漏
    I think I need a boat house
    币值转换
    《C语言程序设计》编程总结汇总
  • 原文地址:https://www.cnblogs.com/dingdingmao/p/3146576.html
Copyright © 2011-2022 走看看