• 随机验证码


    新建立一个生成验证码的页面
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Drawing;
    using NewsService;

    public partial class WebForm_ValidateCode : System.Web.UI.Page
    {
        RandomNumImg RandImg = new RandomNumImg();
        protected void Page_Load(object sender, EventArgs e)
        {
            string checkCode = RandImg.CreateRanmondCode(4);

            Session["CheckCode"] = checkCode;

            CreateRamondImg(checkCode);
        }

        #region 产生随机图片
        /// <summary>
        /// 产生随机图片
        /// </summary>
        /// <param name="checkCode">随机数字</param>
        private void CreateRamondImg(string checkCode)
        {
            Bitmap btimage = new Bitmap(Convert.ToInt32(Math.Ceiling((decimal)(checkCode.Length * 14))), 22);

            Graphics g = Graphics.FromImage(btimage);

            try
            {
                Random Randmond = new Random();

                g.Clear(Color.AliceBlue);

                for (int i = 0; i < 25; i++)
                {
                    int x1 = Randmond.Next(btimage.Width);

                    int x2 = Randmond.Next(btimage.Width);

                    int y1 = Randmond.Next(btimage.Height);

                    int y2 = Randmond.Next(btimage.Height);

                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }

                Font font = new Font("comic sans ms", 12, FontStyle.Bold);

                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, btimage.Width, btimage.Height), Color.Blue, Color.DarkRed, 1.2f, true);

                g.DrawString(checkCode, font, new SolidBrush(Color.Red), 2, 2);

                for (int i = 0; i < 100; i++)
                {
                    int x = Randmond.Next(btimage.Width);

                    int y = Randmond.Next(btimage.Height);

                    btimage.SetPixel(x, y, Color.FromArgb(Randmond.Next()));
                }

                g.DrawRectangle(new Pen(Color.Silver), 0, 0, btimage.Width - 1, btimage.Height - 1);

                System.IO.MemoryStream ms = new System.IO.MemoryStream();

                btimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

                Response.ClearContent();

                Response.ContentType = "image/gif";

                Response.BinaryWrite(ms.ToArray());


            }
            finally
            {
                g.Dispose();

                btimage.Dispose();
            }
        }

        #endregion
    }

    #region 产生随机数据
            /// <summary>
            /// 产生随机数据
            /// </summary>
            /// <param name="codeCount">产生随机数的个数</param>
            /// <returns>返回产生的随机数</returns>
            public string CreateRanmondCode(int codeCount)
            {
                string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";

                string[] allCharArray = allChar.Split(',');

                string ranmodCode = "";

                int temp = -1;

                Random Ranmond = new Random();

                for (int i = 0; i < codeCount; i++)
                {
                    if (temp != -1)
                    {
                        Ranmond = new Random(i * temp * ((int)DateTime.Now.Ticks));
                    }

                    int t = Ranmond.Next(36);

                    if (temp != -1 && temp == t)
                    {
                        return CreateRanmondCode(codeCount);
                    }
                    temp = t;

                    ranmodCode += allCharArray[t];
                }

                return ranmodCode;
            }

            #endregion


    在需要用到验证码的页面添加一个<asp:Image>控件即可,但是要把ImageUrl指向生成验证码的页面

    <asp:Image Runat="server" ID="ImageCheck" ImageUrl="ValidateCode.aspx"></asp:Image>


     

  • 相关阅读:
    VC++中字符串编码的转换
    C与C++之间相互调用
    获取Windows下某进程监听的TCP/UDP端口
    C# byte数组与Image的相互转换
    C# IP地址与整数之间的转换
    NPOI读写Excel
    C#调用ArcGIS REST服务
    粗俗易懂的了解一下PHP的垃圾回收机制(好文章-转)
    从悲观锁和乐观锁的角度怎么避免死锁
    百度地图分布图(百度地图api司机位置实时定位分布图)
  • 原文地址:https://www.cnblogs.com/VirtualMJ/p/524175.html
Copyright © 2020-2023  润新知