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

首頁 > 學院 > 開發設計 > 正文

圖形驗證碼

2019-11-14 14:19:44
字體:
來源:轉載
供稿:網友

新建一個.cs文件用來寫生成圖形驗證碼的方法,返回一個MemoryStream的參數。在Default.aspx中接收輸出就可以了。

1.下面是.cs的代碼

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.IO;/// <summary>///BitmapImage 的摘要說明/// </summary>public class BitmapImage{    public static MemoryStream ImageCheck()    {        Random random = new Random();        //隨機產生4位隨機數        string checkCode = random.Next(1000, 9999).ToString();        //Bitmap是用來指定初始化時文本的大小        Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 20.5)), 22);        Graphics g = Graphics.FromImage(image);        try        {            g.Clear(Color.White);//清空圖片背景色            //畫圖片的背景噪音線這里是4條線            for (int i = 0; i < 4; 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.Black), x1, x2, y1, y2);            }            Font font = new Font("Arial", 12, FontStyle.Bold);            //設置顏色,起始位置等參數            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);            g.DrawString(checkCode, font, brush, 2, 2);            //畫圖片的前景噪音點            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);            MemoryStream ms = new MemoryStream();            image.Save(ms, ImageFormat.Gif);            return ms;        }        finally        {            g.Dispose();            image.Dispose();        }    }}

2.以下是Default.aspx.cs中的代碼

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;public partial class _Default : System.Web.UI.Page{    PRotected void Page_Load(object sender, EventArgs e)    {        MemoryStream ms= BitmapImage.ImageCheck();        Response.ClearContent();        Response.ContentType = "image/Gif";        Response.BinaryWrite(ms.ToArray());    }}

 3.截圖

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金寨县| 安国市| 古丈县| 鹿邑县| 遂平县| 崇礼县| 毕节市| 门头沟区| 凌源市| 弥渡县| 许昌县| 乌鲁木齐市| 宁城县| 涟水县| 云霄县| 临桂县| 哈尔滨市| 赤峰市| 汉源县| 吴川市| 莱西市| 全州县| 宁夏| 宁蒗| 叙永县| 铁岭市| 镇安县| 从化市| 新乐市| 宁津县| 林芝县| 天津市| 玉龙| 专栏| 新绛县| 黔东| 汉川市| 黑山县| 奉贤区| 乌兰浩特市| 屏东县|