Asp.net 生成验证码

public class CreateCodeImageUtil
    {
        public void CreateImage()
        {
            //声明code字体大小
            int fontsize = 15;

            //获取code的值
            string code = GetCode();

            //声明一个图片实例
            Bitmap image = new Bitmap(code.Length * fontsize, 22);

            //声明画图类的实例
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.FromArgb(164, 211, 238));

            //任取四点,画四条贝塞尔样条作为背景噪音线
            Random random = new Random();
            Color color = Color.FromArgb(85, 107, 47);
            Brush brush = new SolidBrush(color);
            Pen pen = new Pen(brush, 2);
            for (int i = 0; i < 3; i++)
            {
                Point p1 = new Point(random.Next(image.Width), random.Next(image.Height));
                Point p2 = new Point(random.Next(image.Width), random.Next(image.Height));
                Point p3 = new Point(random.Next(image.Width), random.Next(image.Height));
                Point p4 = new Point(random.Next(image.Width), random.Next(image.Height));

                g.DrawBezier(pen, p1, p2, p3, p4);
                //g.DrawLine(pen, p3, p4);
            }

            //绘制code
            int angle = random.Next(1, 90);
            Font font = new Font("Arial", fontsize, (FontStyle.Bold | FontStyle.Italic));
            //线性渐变
            LinearGradientBrush textbrush = new LinearGradientBrush
            (new Rectangle(2, 2, image.Width - 4, image.Height - 4),
                Color.DimGray,
                Color.Purple,
                angle);
            g.DrawString(code, font, textbrush, 0, 0);


            //设置前景噪点
            for (int i = 0; i < 20; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);
                image.SetPixel(x, y, Color.FromArgb(142, 142, 56));
            }

            //将图片返回到输出流
            MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ContentType = "image/Gif";
            HttpContext.Current.Response.BinaryWrite(ms.ToArray());

            //释放绘图资源
            g.Dispose();
            image.Dispose();
        }

        public string GetCode()
        {
            try
            {
                Random random = new Random();

                string code = "";
                int num = random.Next(0, 4);
                for (int i = 0; i < num; i++)
                {
                    char chr = (char)(0 + (char)(random.Next() % 10));
                    code += chr;
                }
                for (int i = 0; i < 4 - num; i++)
                {
                    char chr = (char)(A + (char)(random.Next() % 26));
                    code += chr;
                }

                return code;
            }
            catch
            {
                return "";
            }
        }
    }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。