国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

隨機(jī)生成驗證碼

2019-11-14 16:10:21
字體:
供稿:網(wǎng)友
using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.IO;using System.Linq;namespace Letter.Data.Command{    public class ValidateCode    {        /// <summary>        /// 創(chuàng)建驗證碼的圖片        /// </summary>        /// <param name="validateCode">驗證碼</param>        public byte[] CreateValidateGraphic(string validateCode)        {                        Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22);            Graphics g = Graphics.FromImage(image);            try            {                //生成隨機(jī)生成器                Random random = new Random();                //清空圖片背景色                g.Clear(Color.White);                //畫圖片的干擾線                for (int i = 0; i < 25; i++)                {                    int x1 = random.Next(image.Width);                    int x2 = random.Next(image.Width);                    int y1 = random.Next(image.Height);                    int y2 = random.Next(image.Height);                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);                }                Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),                 Color.Blue, Color.DarkRed, 1.2f, true);                g.DrawString(validateCode, font, brush, 3, 2);                //畫圖片的前景干擾點(diǎn)                for (int i = 0; i < 100; i++)                {                    int x = random.Next(image.Width);                    int y = random.Next(image.Height);                    image.SetPixel(x, y, Color.FromArgb(random.Next()));                }                //畫圖片的邊框線                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);                //保存圖片數(shù)據(jù)                MemoryStream stream = new MemoryStream();                image.Save(stream, ImageFormat.Jpeg);                //輸出圖片流                return stream.ToArray();            }            finally            {                g.Dispose();                image.Dispose();            }        }        /// <summary>        /// 生成數(shù)字隨機(jī)碼        /// </summary>        /// <param name="count">隨機(jī)位數(shù)</param>        /// <returns></returns>        public string CreateValidateCode(int count)        {            Random r = new Random();            string code = string.Empty;            for (int i = 0; i < count; i++)            {                code += r.Next(0, 9)+"";            }            return code;        }    }}

 

controller調(diào)用

/// <summary>        /// 獲取驗證碼        /// </summary>        /// <returns></returns>        public ActionResult GetValidateCode()        {            ValidateCode vCode = new ValidateCode();            string code = vCode.CreateValidateCode(4);            session["ValidateCode"] = code;            byte[] bytes = vCode.CreateValidateGraphic(code);            return File(bytes, @"image/jpeg");        }

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 樟树市| 鄄城县| 泸州市| 宁武县| 鹿泉市| 迭部县| 东乡族自治县| 桃园县| 吴江市| 商洛市| 邹平县| 宣恩县| 疏附县| 麦盖提县| 南平市| 苍梧县| 军事| 同德县| 夏河县| 平南县| 黄浦区| 祥云县| 丹江口市| 玛沁县| 浦东新区| 龙江县| 邵阳市| 安丘市| 玛纳斯县| 临城县| 郸城县| 荥经县| 天气| 新民市| 张北县| 林周县| 沙洋县| 庆城县| 惠来县| 丽江市| 上思县|