• asp.net生成验证码


    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.IO;
    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 System.Drawing.Drawing2D;
    using System.Drawing.Imaging;

    namespace ASPNET.wzh.shopDoNet
    {
        public partial class getCode : System.Web.UI.Page
        {
            private Bitmap validateimage;
            private Graphics g;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    string VNum = MakeValidateCode();
                    Session["getCode"] = VNum;
                    ValidateCode(VNum);
                }
            }

            public void ValidateCode(string VNum)
            {
                validateimage = new Bitmap(35, 12, PixelFormat.Format24bppRgb);
                g = Graphics.FromImage(validateimage);

                g.DrawString(VNum, new Font("宋体", 10), new SolidBrush(Color.White), new PointF(0, 0));
                g.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(110, 20), Color.FromArgb(138, 238, 238, 255), Color.FromArgb(38, 238, 238, 255)), 0, 0, 120, 30);
                g.Save();
                MemoryStream ms = new MemoryStream();
                validateimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();
                Response.ContentType = "image/gif";
                Response.BinaryWrite(ms.ToArray());
                Response.End();
            }

            string MakeValidateCode()
            {
                char[] s = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
                string num = "";
                Random r = new Random();
                for (int i = 0; i < 4; i++)
                {
                    num += s[r.Next(0, s.Length)].ToString();
                }
                return num;
            }
        }

    }

    另外在需要显示验证码的地方加入

    <asp:Image ID="imgGetCode" runat="server" ImageUrl="getCode.aspx" />

     
  • 相关阅读:
    SQL中的cast和convert用法示例
    SpendidCRM:在adminlayout中修改了视图上的字段后,用MyGeneration生成修改脚本
    [转]ASP.NET数据绑定方法
    商城 5051课 纪要
    .net判断用户是否已登录
    Jquery 监视按键,按下回车键触发某方法
    Sharepoint中三个强力的工具(WPManager, OSSEventManager, FriendlyQuery),另增清除已删除用户
    错误处理:此网页不允许使用基本类型"System.Web.UI.MasterPage”该类型未注册为安全类型
    MOSS页面显示“根级别上的数据无效”
    读取/设置超链接或图片类型栏的值
  • 原文地址:https://www.cnblogs.com/zhuawang/p/704269.html
Copyright © 2020-2023  润新知