在前台放在如下四个控件

<div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>       <%--TextBox-等待输入验证码--%>
        <asp:Image ID="Image1" runat="server" ImageUrl="YZM.aspx" />     <%--Image-显示验证码图片--%>
<asp:Button ID="Button1" runat="server" Text="提交验证" />       <%--Button-提交进行验证--%> <asp:Label ID="Label1" runat="server" Text="验证中..."></asp:Label>   <%--Label-显示验证结果--%> </div>

C#-WebForm-★ 制作图片验证码 ★

此时验证码为空,不显示任何东西

 

步骤:

一、给验证码图片控件加一个连接,此连接是.aspx网页,此网页不需要前台,只需要打开时后台做一个验证码图片展示出来即可

<asp:Image ID="Image1" runat="server" ImageUrl="YZM.aspx" />

二、在YZM.aspx后台中制作简单验证码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class YZM : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Random r = new Random();

        //制作“位图”(指定长宽的矩形区域)
        Bitmap img = new Bitmap(60,30);

        //准备制作-
        //设定画布
        Graphics g = Graphics.FromImage(img);
        //输出的字符串
        string all = "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        string s = "";
        for (int i = 1; i <= 4; i++)
        {
            s += all.Substring(r.Next(all.Length),1);
        }
        //字符串的字体
        Font f=new Font ("微软雅黑",16);
        //字体的颜色
        Brush b=new SolidBrush(Color.Red);
        //起始位置
        PointF p=new PointF (3,3);
        //进行制作-
        g.DrawString(s, f, b, p);

        //进行保存(保存到流中)
        img.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Png);
        Response.End();
    }
}
YZM.aspx后台代码

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案