提取自Discuz NT 的验证码生成

  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Collections;
  5using System.Web;
  6using System.Web.Security;
  7using System.Web.UI;
  8using System.Web.UI.WebControls;
  9using System.Web.UI.WebControls.WebParts;
 10using System.Web.UI.HtmlControls;
 11using System.IO;
 12using System.Drawing;
 13using System.Drawing.Drawing2D;
 14using System.Drawing.Imaging;
 15using System.Drawing.Text;
 16using System.Security.Cryptography;
 17
 18public partial class inc_VerifyCode : System.Web.UI.Page
 19{
 20    protected void Page_Load(object sender, EventArgs e)
 21    {
 22        GenerateImage("2312"15540, Color.White, 111111);
 23    }

 24
 25
 26    private static byte[] randb = new byte[4];
 27    private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
 28
 29    private static Font[] fonts = {
 30                                        new Font(new FontFamily("Times New Roman"), 20 + Next(4),System.Drawing.FontStyle.Bold),
 31                                        new Font(new FontFamily("Georgia"), 20 + Next(4),System.Drawing.FontStyle.Bold),
 32                                        new Font(new FontFamily("Arial"), 20 + Next(4),System.Drawing.FontStyle.Bold),
 33                                        new Font(new FontFamily("Comic Sans MS"), 20 + Next(4),System.Drawing.FontStyle.Bold)
 34                                     }
;
 35    /// <summary>
 36    /// 获得下一个随机数
 37    /// </summary>
 38    /// <param name="max">最大值</param>
 39    /// <returns></returns>

 40    private static int Next(int max)
 41    {
 42        rand.GetBytes(randb);
 43        int value = BitConverter.ToInt32(randb, 0);
 44        value = value % (max + 1);
 45        if (value < 0)
 46            value = -value;
 47        return value;
 48    }

 49
 50    /// <summary>
 51    /// 获得下一个随机数
 52    /// </summary>
 53    /// <param name="min">最小值</param>
 54    /// <param name="max">最大值</param>
 55    /// <returns></returns>

 56    private static int Next(int min, int max)
 57    {
 58        int value = Next(max - min) + min;
 59        return value;
 60    }

 61
 62
 63    public void GenerateImage(string code, int width, int height, Color bgcolor, int textcolor)
 64    {
 65
 66
 67        Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
 68
 69        Graphics g = Graphics.FromImage(bitmap);
 70        Rectangle rect = new Rectangle(00, width, height);
 71        g.SmoothingMode = SmoothingMode.AntiAlias;
 72
 73        g.Clear(bgcolor);
 74
 75        //int emSize = Next(3) + 15;//(int)((width - 20) * 2 / text.Length);
 76        //FontFamily family = fonts[Next(fonts.Length - 1)];
 77        //Font font = new Font(family, emSize, FontStyle.Bold);
 78
 79        //SizeF measured = new SizeF(0, 0);
 80        //SizeF workingSize = new SizeF(width, height);
 81        //while (emSize > 2 && (measured = g.MeasureString(code, font)).Width > workingSize.Width || measured.Height > workingSize.Height)
 82        //{
 83        //    font.Dispose();
 84        //    font = new Font(family, emSize -= 2);
 85        //}
 86        int fixedNumber = textcolor == 2 ? 60 : 0;
 87
 88        SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(100), Next(100)));
 89        for (int x = 0; x < 3; x++)
 90        {
 91            Pen linePen = new Pen(Color.FromArgb(Next(150+ fixedNumber, Next(150+ fixedNumber, Next(150+ fixedNumber), 1);
 92            g.DrawLine(linePen, new PointF(0.0F + Next(20), 0.0F + Next(height)), new PointF(0.0F + Next(width), 0.0F + Next(height)));
 93        }

 94
 95
 96        Matrix m = new Matrix();
 97        for (int x = 0; x < code.Length; x++)
 98        {
 99            m.Reset();
100            m.RotateAt(Next(30- 15new PointF(Convert.ToInt64(width * (0.10 * x)), Convert.ToInt64(height * 0.5)));
101            g.Transform = m;
102            drawBrush.Color = Color.FromArgb(Next(150+ fixedNumber + 20, Next(150+ fixedNumber + 20, Next(150+ fixedNumber + 20);
103            PointF drawPoint = new PointF(0.0F + Next(4+ x * 203.0F + Next(3));
104            g.DrawString(Next(1== 1 ? code[x].ToString() : code[x].ToString().ToUpper(), fonts[Next(fonts.Length - 1)], drawBrush, drawPoint);
105            g.ResetTransform();
106        }

107
108
109
110        double distort = Next(510* (Next(10== 1 ? 1 : -1);
111
112        using (Bitmap copy = (Bitmap)bitmap.Clone())
113        {
114            for (int y = 0; y < height; y++)
115            {
116                for (int x = 0; x < width; x++)
117                {
118                    int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 84.5)));
119                    int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 54.5)));
120                    if (newX < 0 || newX >= width)
121                        newX = 0;
122                    if (newY < 0 || newY >= height)
123                        newY = 0;
124                    bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
125                }

126            }

127        }

128
129
130        //g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);
131
132
133
134        drawBrush.Dispose();
135        g.Dispose();
136
137        System.IO.MemoryStream ms = new System.IO.MemoryStream();
138        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
139        Response.ClearContent();
140        Response.ContentType = "image/Gif";
141        Response.BinaryWrite(ms.ToArray());
142    }

143
144}

145
原文地址:https://www.cnblogs.com/future/p/1242999.html