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

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Drawing;
    using System.Drawing.Imaging;
    
    namespace test
    {
        public partial class verCode : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    GenerateCode();
                }
            }
    
    
    
            private void GenerateCode()
            {
                Random r = new Random();
                string code = "";
    
                for (int i = 0; i < 5; i++)
                {
                    code += r.Next(0, 10);
                }
    
                //字体
                string[] fonts = { "微软雅黑","宋体","黑体","仿宋","隶书"};
                //字体颜色
                Color[] colors = { Color.Red,Color.Black,Color.Blue,Color.Orange,Color.Yellow };
                //创建位图
                Bitmap bmp = new Bitmap(100, 30);
                //创建GDI
                Graphics g = Graphics.FromImage(bmp);
                //清空画布
                g.Clear(Color.White);
    
                //创建画笔
                Pen pen = new Pen(Color.Green);
                //设置线的宽度
                pen.Width = 2;
    
                //划线添加干扰
                for (int i = 0; i < 50; i++)
                {
                    Point p1 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
    
                    Point p2 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
                    //划线
                    g.DrawLine(pen, p1, p2);
                }
    
                for (int i = 0; i < 5; i++)
                {
                    Font font = new Font(fonts[r.Next(0,5)],20,FontStyle.Bold);
    
                    SolidBrush brush = new SolidBrush(colors[r.Next(0,5)]);
    
                    Point p = new Point(i*20,0);
                    //画文本内容
                    g.DrawString(code[i].ToString(),font,brush,p);
                    
                }
                
                //添加斑点
    
                for (int i = 0; i < 500; i++)
                {
                    //对位图添加斑点
                    bmp.SetPixel(r.Next(0,bmp.Width),r.Next(0,bmp.Height),Color.Lime);
                }
    
                
                //保存位图
                bmp.Save(Response.OutputStream, ImageFormat.Gif);
    
                //设置相应类型
                Response.ContentType = "image/gif";
    
                bmp.Dispose();
    
                g.Dispose();
    
    
            }
        }
    }
  • 相关阅读:
    [贪心] JZOJ P3757 随机生成器
    [kmp] JZOJ P3756 动物园
    [kmp] JZOJ P3756 动物园
    [记忆化搜索] JZOJ P3767 路径
    [dfs序][线段树][并查集] JZOJ P3766 大融合
    [归并][随机算法] JZOJ P3765 想法
    [枚举][dfs] JOZJ P3749 Fox and City
    [hash] JZOJ P3669 抄卡组
    [dfs][图] 洛谷 P1330 封锁阳光大学
    [并查集]NOIP 2015 Day1 信息传递
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/4041689.html
Copyright © 2011-2022 走看看