zoukankan      html  css  js  c++  java
  • ASP.Net MVC生成验证码

    /// <summary>
            /// 数值验证码
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Button1_Click(object sender, EventArgs e)
            {
                string sourcecode = "0123456789";
                string code = "";

                Random random = new Random();
                for (int i = 0; i < 6; i++)
                {
                    int flag = random.Next(0, 9);
                    code += sourcecode.Substring(flag, 1);
                }
                label1.Text = code;
            }

            /// <summary>
            /// 数值+字母验证码
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Button2_Click(object sender, EventArgs e)
            {
                string sourcecode = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                string code = "";
                //随机获取
                Random random = new Random();
                for (int i = 0; i < 6; i++)
                {
                    int flag = random.Next(0, 61);
                    code += sourcecode.Substring(flag, 1);
                }
                label2.Text = code;
            }

            /// <summary>
            /// 图片验证码
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Button3_Click(object sender, EventArgs e)
            {
                string sourcecode = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                string code = "";

                //随机获取
                Random random = new Random();
                for (int i = 0; i < 6; i++)
                {
                    int flag = random.Next(0, 61);
                    code += sourcecode.Substring(flag, 1);
                }

                //定义一个画板
                Bitmap bitmap = new Bitmap(160, 50);//设置的是宽高
                //定义画布
                Graphics graphics = Graphics.FromImage(bitmap);
                //在画布上设置背景颜色
                graphics.FillRectangle(new SolidBrush(Color.Red), 0, 0, 160, 50);
                //设置字体
                Font font = new Font(new FontFamily("微软雅黑"), 15.0f);

                //画干扰线
                for (int i = 0; i < 6; i++)
                {
                    Pen pen = new Pen(new SolidBrush(Color.Yellow), 1);
                    Point point = new Point(random.Next(0, 160), random.Next(2, 80));
                    Point point1 = new Point(random.Next(0, 160), random.Next(2, 90));
                    graphics.DrawLine(pen, point, point1);
                }
                //画图
                graphics.DrawString(code, font, new SolidBrush(Color.Green), new PointF(10, 10));
                //保存图片
                using (FileStream stream = new FileStream(@"C:\Users\张艳薇\Desktop\needyou\code.png", FileMode.Create, FileAccess.ReadWrite))
                {
                    bitmap.Save(stream, ImageFormat.Png);
                }
            }

  • 相关阅读:
    二十八 .Django中模型类中Meta元对象了解
    【POJ2376】Cleaning Shifts
    【CF600E】Lomsat gelral
    【hiho1035】自驾旅行III
    【模板】manachar
    【hiho1065】全图传送
    【洛谷P1450】硬币购物
    【HDU2204】Eddy's爱好
    【CF208E】Blood Cousins
    【CF451E】Devu and Flowers
  • 原文地址:https://www.cnblogs.com/XJNB/p/13141785.html
Copyright © 2011-2022 走看看