惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
Scott Helme
Scott Helme
NISL@THU
NISL@THU
Cisco Talos Blog
Cisco Talos Blog
C
Cybersecurity and Infrastructure Security Agency CISA
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
J
Java Code Geeks
U
Unit 42
The GitHub Blog
The GitHub Blog
H
Help Net Security
T
Tenable Blog
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
L
Lohrmann on Cybersecurity
T
Threatpost
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
I
InfoQ
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog
L
LINUX DO - 最新话题
K
Kaspersky official blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Threat Research - Cisco Blogs
C
Check Point Blog
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Help Net Security
Help Net Security
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
Y
Y Combinator Blog
N
News | PayPal Newsroom
M
MIT News - Artificial intelligence
Latest news
Latest news
H
Hacker News: Front Page
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
I
Intezer
爱范儿
爱范儿
F
Fortinet All Blogs
P
Palo Alto Networks Blog
C
CERT Recently Published Vulnerability Notes

博客园 - 搞IT的狐狸

ASP.Net动态使用CSS - 搞IT的狐狸 - 博客园 SqlDataReader或DATATABLE DATASET 到EXCEL [转载]ROW_NUM实现分页 (转载)ASP.NET本地化 VS2005 TreeView 的 CheckBox 被点击时的引发页面回发事件 (转发) ASP.NET 2.0,C#----利用GridView控件导出其他文件(导出Excel,导出Word文件) 什么是SHTML 如何使用IFRAM JS来刷新UpdatePanel [北京]招聘 网站设计师 我这半个月的代码总结(小技巧,小代码)之二 我这半个月的代码总结(小技巧,小代码)之一 用Themes实现网站换肤 将数据控件(如GridView)的内容转化成Excel格式文件 ASP.NET新手实用技巧!(C#) 学历不高!写给自己!自勉! 一张笑死我又让我辛酸的图片!开发人员的辛酸! 服务器刚开,凑个沙发!.NET几点你应该知道的细节! 求动态绑定控件ID的值的方法!着急!
随即验证码实现(根据韩现龙代码修改)
搞IT的狐狸 · 2008-01-23 · via 博客园 - 搞IT的狐狸

 protected void Page_Load(object sender, EventArgs e)
    {
     Encoding gb = Encoding.GetEncoding("gb2312");
 
         //调用函数产生4个随机数字编码
         object[] bytes = CreateRegionCode(4);
 
        
         string str1 = bytes[0].ToString();
         string str2 = bytes[1].ToString();
         string str3 = bytes[2].ToString();
         string str4 = bytes[3].ToString();
 
         //输出的控制台
       
         // Response.Write(str1 + str2 + str3 + str4);
        //  return;

         string srs = str1 + str2 + str3 + str4;
         Bitmap srBmp = srBitmap(srs);
         System.IO.MemoryStream srMS = new System.IO.MemoryStream();
         srBmp.Save(srMS, System.Drawing.Imaging.ImageFormat.Gif);
         Response.ClearContent();
         Response.ContentType = "image/gif";
         Response.BinaryWrite(srMS.ToArray());
         srBmp.Dispose();

    }


     /// <summary>
     /// 根据指定参数返回BitMap对象
     /// 引用如下:
     /// using System.Drawing;
     /// 调用例子如下:
     ///     eg1、保存为图象文件为
     ///     Bitmap srBmp = srBitmap(srs);
     ///     srBmp.Save(Directory.GetCurrentDirectory() + "\\srs.gif", System.Drawing.Imaging.ImageFormat.Gif);
     ///     srBmp.Dispose();
     ///     eg2。网页中调用方式如下
     ///     Bitmap srBmp = srBitmap(srs);
     ///     System.IO.MemoryStream srMS = new System.IO.MemoryStream();
     ///     srBmp.Save(srMS,System.Drawing.Imaging.ImageFormat.Gif);
     ///     Response.ClearContent();
     ///     Response.ContentType = "image/gif";
     ///     Response.BinaryWrite(srMS.ToArray());
     ///     srBmp.Dispose();
     /// </summary>
     /// <param name="srs"></param>
     /// <returns></returns>


 public static Bitmap srBitmap(string srs)
     {
         //定义图片弯曲的角度
        int srseedangle = 40;
        //定义图象
        Bitmap srBmp = new Bitmap(srs.Length * 20, 30);
         //画图
         Graphics srGraph = Graphics.FromImage(srBmp);
         //清空图象
         srGraph.Clear(Color.AliceBlue);
         //给图象画边框
         srGraph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, srBmp.Width - 1, srBmp.Height - 1);
         //定义随即数
         Random srRandom = new Random();
         //定义画笔
         Pen srPen = new Pen(Color.LightBlue, 0);
         //画噪点
        for (int i = 0; i < 100; i++)
         {
             srGraph.DrawRectangle(srPen, srRandom.Next(1, srBmp.Width - 2), srRandom.Next(1, srBmp.Height - 2), 1, 1);
         }
       //将字符串转化为字符数组
         char[] srchars = srs.ToCharArray();
         //封状文本
         StringFormat srFormat = new StringFormat(StringFormatFlags.NoClip);
         //设置文本垂直居中
         srFormat.Alignment = StringAlignment.Center;
         //设置文本水平居中
         srFormat.LineAlignment = StringAlignment.Center;
         //定义字体颜色
         Color[] srColors ={ Color.Black, Color.Red, Color.DarkBlue, Color.Blue, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
         //定义字体
         string[] srFonts ={ "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
         //循环画出每个字符
         for (int i = 0, j = srchars.Length; i < j; i++)
         {
             //定义字体 参数分别为字体样式 字体大小 字体字形
          
           Font srFont = new Font(srFonts[srRandom.Next(5)], 12, FontStyle.Regular);
            //填充图形
            Brush srBrush = new SolidBrush(srColors[srRandom.Next(7)]);
            //定义坐标
            Point srPoint = new Point(16, 16);
            //定义倾斜角度
            float srangle = srRandom.Next(-srseedangle, srseedangle);
           //倾斜
           srGraph.TranslateTransform(srPoint.X, srPoint.Y);
            srGraph.RotateTransform(srangle);
            //填充字符
            srGraph.DrawString(srchars[i].ToString(), srFont, srBrush, 1, 1, srFormat);
            //回归正常
            srGraph.RotateTransform(-srangle);
            srGraph.TranslateTransform(2, -srPoint.Y);
        }
       srGraph.Dispose();
       return srBmp;
    }

    public static object[] CreateRegionCode(int strlength)
    {

        string[] rBase = new string[10] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        Random rnd = new Random();
      


        //定义一个object数组用来

        object[] bytes = new object[strlength];
      

        for (int i = 0; i < strlength; i++)
        {
            int r1 = rnd.Next(0, 10);
            string str_r1 = rBase[r1].Trim();
            rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks + i));//更换随机数发生器的种子避免产生重复值

       //定义两个字节变量存储产生的随机数字区位码
           byte byte1 = Convert.ToByte(str_r1, 16);
        
          

           //将产生的一个汉字的字节数组放入object数组中
            bytes.SetValue(byte1,i);

        }

        return bytes;

    }