zoukankan      html  css  js  c++  java
  • 验证码生成

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Web;
    using System.IO;
    
    namespace Helper
    {
        public class VCode
        {
            /// <summary>
            /// 生成验证码图片 字节数组
            /// </summary>
            /// <returns></returns>
            public byte[] GetVCode()
            {
                using (Image img = new Bitmap(80, 30))
                {
                    string strCode = GetRandomStr();
                    HttpContext.Current.Session["vcode"] = strCode;
                    using (Graphics g = Graphics.FromImage(img))
                    {
                        g.Clear(Color.White);
                        g.DrawRectangle(Pens.Blue, 0, 0, img.Width - 1, img.Height - 1);
                        DrawPoint(g);
                        g.DrawString(strCode, new Font("微软雅黑", 15), Brushes.Blue, new PointF(5, 2));
                        DrawPoint(g);
                        using (System.IO.MemoryStream ms = new MemoryStream())
                        {
                            //将图片 保存到内存流中
                            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                            //将内存流 里的 数据  转成 byte 数组 返回
                            return ms.ToArray();
                        }
                    }
                }
                return null;
            }
    
    
            Random random = new Random();
    
            string GetRandomStr()
            {
                string str = string.Empty;
                string[] strArr = { "a", "b", "b", "", "1", "2", "", "3", "4", "5", "6", "7", "8", "9", "0" };
                for (int i = 0; i < 5; i++)
                {
                    int index = random.Next(strArr.Length);
                    str += strArr[index];
                }
                return str;
            }
    
            void DrawPoint(Graphics g)
            {
                Pen[] pens = { Pens.Blue, Pens.Black, Pens.Red, Pens.Green };
                Point p1;
                Point p2;
                int length = 1;
                for (int i = 0; i < 50; i++)
                {
                    p1 = new Point(random.Next(79), random.Next(29));
                    p2 = new Point(p1.X - length, p1.Y - length);
                    length = random.Next(5);
                    g.DrawLine(pens[random.Next(pens.Length)], p1, p2);
                }
            }
        }
    }
    生成验证码
    /// <summary>
            /// 生成验证码
            /// </summary>
            /// <returns></returns>
            public ActionResult VCode()
            {
                Helper.VCode v = new Helper.VCode();
                byte[] arrImg = v.GetVCode();
                return File(arrImg, "image/jpeg");
            }
  • 相关阅读:
    随笔:我为什么要写博客?
    用纯C语言写的一个植物大战僵尸的外挂
    方法:如何获取操作系统所有分区(逻辑驱动器)
    Srping syntactically incorrect.错误记录
    重建项目报错
    easyui datagrid 跨页选择
    CentOS 6编译安装ipvsadm和keepalived
    CentOS 6下ActiveMQ 5.5安装及使用MySQL
    extjs 4中TreePanel和GridPanel使用
    Linux安装性能问题
  • 原文地址:https://www.cnblogs.com/xiaoyangshu/p/12297726.html
Copyright © 2011-2022 走看看