又一个验证码程序

一个验证码程序,今天在网上无意中看到的,先收藏了:

  1. ValidCode
  2. using System;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Text;
  6. using System.Drawing.Imaging;
  7. using System.Security.Cryptography;
  8. using System.Drawing.Drawing2D;
  9. public partial class ValidCode : System.Web.UI.Page
  10. {
  11.     protected override void OnInit(EventArgs e)
  12.     {
  13.         base.OnInit(e);
  14.         string authStr = CreateAuthStr(4);
  15.         VerifyImage verifyimg = new VerifyImage(authStr, 90, 50);
  16.         System.Drawing.Bitmap image = verifyimg.Image;
  17.         System.Web.HttpContext.Current.Response.ContentType = "image/pjpeg";
  18.         //Session["AuthStr"] = authStr.ToLower();
  19.         image.Save(this.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
  20.     }
  21.     /**//// <summary>
  22.     /// 产生验证码
  23.     /// </summary>
  24.     /// <returns>验证码</returns>
  25.     public static string CreateAuthStr(int len)
  26.     {
  27.         int number;
  28.         StringBuilder checkCode = new StringBuilder();
  29.         Random random = new Random();
  30.         for (int i = 0; i < len; i++)
  31.         {
  32.             number = random.Next();
  33.             if (number % 2 == 0)
  34.             {
  35.                 checkCode.Append((char)('0' + (char)(number % 10)));
  36.             }
  37.             else
  38.             {
  39.                 checkCode.Append((char)('A' + (char)(number % 26)));
  40.             }
  41.         }
  42.         return checkCode.ToString();
  43.     }
  44. }
  45. /**//// <summary>
  46. /// 验证码图片类
  47. /// </summary>
  48. public class VerifyImage
  49. {
  50.     /**//// <summary>
  51.     /// 要显示的文字
  52.     /// </summary>
  53.     public string Text
  54.     {
  55.         get { return this.text; }
  56.     }
  57.     /**//// <summary>
  58.     /// 图片
  59.     /// </summary>
  60.     public Bitmap Image
  61.     {
  62.         get { return this.image; }
  63.     }
  64.     /**//// <summary>
  65.     /// 宽度
  66.     /// </summary>
  67.     public int Width
  68.     {
  69.         get { return this.width; }
  70.     }
  71.     /**//// <summary>
  72.     /// 高度
  73.     /// </summary>
  74.     public int Height
  75.     {
  76.         get { return this.height; }
  77.     }
  78.     private string text;
  79.     private int width;
  80.     private int height;
  81.     private Bitmap image;
  82.     private static byte[] randb = new byte[4];
  83.     private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
  84.     /**//// <summary>
  85.     /// 构造函数
  86.     /// </summary>
  87.     /// <param name="code">要显示的验证码</param>
  88.     /// <param name="width">宽度</param>
  89.     /// <param name="height">高度</param>
  90.     public VerifyImage(string code, int width, int height)
  91.     {
  92.         this.text = code;
  93.         this.width = width;
  94.         this.height = height;
  95.         this.GenerateImage();
  96.     }
  97.     ~VerifyImage()
  98.     {
  99.         Dispose(false);
  100.     }
  101.     public void Dispose()
  102.     {
  103.         GC.SuppressFinalize(this);
  104.         this.Dispose(true);
  105.     }
  106.     protected virtual void Dispose(bool disposing)
  107.     {
  108.         if (disposing)
  109.             this.image.Dispose();
  110.     }
  111.     private FontFamily[] fonts = {
  112.                                          new FontFamily("Times New Roman"),
  113.                                          new FontFamily("Georgia"),
  114.                                          new FontFamily("Arial"),
  115.                                          new FontFamily("Comic Sans MS")
  116.                                      };
  117.     public static int Next()
  118.     {
  119.         rand.GetBytes(randb);
  120.         int value = BitConverter.ToInt32(randb, 0);
  121.         if (value < 0) value = -value;
  122.         return value;
  123.     }
  124.     public static int Next(int max)
  125.     {
  126.         rand.GetBytes(randb);
  127.         int value = BitConverter.ToInt32(randb, 0);
  128.         value = value % (max + 1);
  129.         if (value < 0) value = -value;
  130.         return value;
  131.     }
  132.     public static int Next(int min, int max)
  133.     {
  134.         int value = Next(max - min) + min;
  135.         return value;
  136.     }
  137.     /**//// <summary>
  138.     /// 生成验证码图片
  139.     /// </summary>
  140.     private void GenerateImage()
  141.     {
  142.         Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);
  143.         Graphics g = Graphics.FromImage(bitmap);
  144.         Rectangle rect = new Rectangle(0, 0, this.width, this.height);
  145.         g.SmoothingMode = SmoothingMode.AntiAlias;
  146.         g.Clear(Color.White);
  147.         int emSize = Next(3) + 18;//(int)((this.width - 20) * 2 / text.Length);
  148.         FontFamily family = fonts[Next(fonts.Length - 1)];
  149.         Font font = new Font(family, emSize, FontStyle.Bold);
  150.         SizeF measured = new SizeF(0, 0);
  151.         SizeF workingSize = new SizeF(this.width, this.height);
  152.         while (emSize > 2 && (measured = g.MeasureString(text, font)).Width > workingSize.Width || measured.Height > workingSize.Height)
  153.         {
  154.             font.Dispose();
  155.             font = new Font(family, emSize -= 2);
  156.         }
  157.         SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(100), Next(100)));
  158.         for (int x = 0; x < 3; x++)
  159.         {
  160.             Pen linePen = new Pen(Color.FromArgb(Next(150), Next(150), Next(150)), 1);
  161.             g.DrawLine(linePen, new PointF(0.0F + Next(20), 0.0F + Next(this.height)), new PointF(0.0F + Next(this.width), 0.0F + Next(this.height)));
  162.         }
  163.         for (int x = 0; x < this.text.Length; x++)
  164.         {
  165.             drawBrush.Color = Color.FromArgb(Next(150) + 20, Next(150) + 20, Next(150) + 20);
  166.             PointF drawPoint = new PointF(0.0F + Next(4) + x * 15, 8.0F + Next(4));
  167.             g.DrawString(this.text[x].ToString(), font, drawBrush, drawPoint);
  168.         }
  169.         double distort = Next(5, 10) * (Next(10) == 1 ? 1 : -1);
  170.         using (Bitmap copy = (Bitmap)bitmap.Clone())
  171.         {
  172.             for (int y = 0; y < height; y++)
  173.             {
  174.                 for (int x = 0; x < width; x++)
  175.                 {
  176.                     int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 84.0)));
  177.                     int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 54.0)));
  178.                     if (newX < 0 || newX >= width) newX = 0;
  179.                     if (newY < 0 || newY >= height) newY = 0;
  180.                     bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
  181.                 }
  182.             }
  183.         }
  184.         //g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);
  185.         font.Dispose();
  186.         drawBrush.Dispose();
  187.         g.Dispose();
  188.         this.image = bitmap;
  189.     }
  190. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值