• 随机产生验证码图片---参考代码


    前端:

    <tr>
    <td style="height: 20px" ><div align="center"> 验证码:</div></td>
    <td align="left" style=" 277px; height: 20px;" ><asp:TextBox ID="txtCode" runat="server" Height="25px" Width="112px"></asp:TextBox></td>
    <td align="center" style="height: 20px" ><asp:Image ID="Image1" runat="server" Width="100px" BorderColor="Gray" BorderWidth="1px" Height="32px" ImageUrl="~/Image.aspx" /></td>
    <td align="center" style="height: 20px" ><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtCode"
    Display="Dynamic" ErrorMessage="请输入验证码" ForeColor="DimGray" Font-Size="10pt">*</asp:RequiredFieldValidator>
    &nbsp;</td>
    </tr> 

    Image.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Image.aspx.cs" Inherits="Image" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        </div>
        </form>
    </body>
    </html>

    Image.aspx.cs

    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.IO;
    using System.Drawing.Imaging;
    using System.Drawing;
    public partial class Image : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string tmp = RndNum(Convert.ToInt16(6));
            Session["verify"] = tmp;
            ValidateCode(tmp);
        }
        private void ValidateCode(string VNum)
        {
            Bitmap Img = null;
            Graphics g = null;
            MemoryStream ms = null;
            int gheight = VNum.Length * 9;
            Img = new Bitmap(gheight, 18);
            g = Graphics.FromImage(Img);
            //背景颜色
            g.Clear(Color.WhiteSmoke);
            //文字字体
            Font f = new Font("Tahoma", 10);
            //文字颜色
            SolidBrush s = new SolidBrush(Color.Red);
            g.DrawString(VNum, f, s, 3, 3);
            ms = new MemoryStream();
            Img.Save(ms, ImageFormat.Jpeg);
            Response.ClearContent();
            Response.ContentType = "image/Jpeg";
            Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            Img.Dispose();
            Response.End();
        }
        private string RndNum(int VcodeNum)
        {
            string MaxNum = "";
            string MinNum = "";
            for (int i = 0; i < 5; i++)//这里的4是验证码的位数
            {
                MaxNum = MaxNum + "5";
            }
            MinNum = MaxNum.Remove(0, 1);
            Random rd = new Random();
            string VNum = Convert.ToString(rd.Next(Convert.ToInt32(MinNum), Convert.ToInt32(MaxNum)));
            return VNum;
        }
    }
  • 相关阅读:
    java.lang.NoClassDefFoundError: TagSupport 错误
    ${pageContext.request.contextPath}的作用
    JS获取table表格任意单元格值
    数据库导出为Excel
    requestScope含义
    每种创伤,都是另一种成熟
    关于AJAX
    the public type xxx must be defined in its own file
    MyEclipse取消Show in Breadcrumb的方法
    Sql2005里获取表的结构SQL
  • 原文地址:https://www.cnblogs.com/eric-qin/p/4329392.html
Copyright © 2020-2023  润新知