zoukankan      html  css  js  c++  java
  • 如何在asp.net中动态生成验证码(转)

    现在越来越多的网站喜欢搞个验证码出来,而且各个语言基本上都能做到,今天我来一个C#写的!

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;

    //建立位图对象

    public void randomNumber()

    {
       Bitmap newBitmap = new Bitmap(36,16,PixelFormat.Format32bppArgb);
       //根据上面创建的位图对象创建绘图面
       Graphics g = Graphics.FromImage(newBitmap);
       //以指定的颜色填充矩形区
       g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0,0,36,16));
       //创建字体对象
       Font textFont = new Font("Times New Roman",10);
       //创建RectangleF结构指定一个区域
       RectangleF rectangle = new RectangleF(0,0,36,16);
       //创建随机数对象
       Random rd = new Random();
       //取得随机数
       int valationNo = 1000 + rd.Next(8999);
       //使用指定的颜色填充上面RectangleF结构指定的矩形区域
       g.FillRectangle(new SolidBrush(Color.BurlyWood), rectangle);
       //在上面填充的矩形区域中填充上面生成的随机数
       g.DrawString(valationNo.ToString(), textFont, new SolidBrush(Color.Blue), rectangle);
       //把创建的位图保存到指定的路径
       newBitmap.Save(Server.MapPath("img")+"\\Img.gif", ImageFormat.Gif);

    }

    生成以后在前台页面里引入这个图片的地址就可以了!

  • 相关阅读:
    大数据笔记
    关于服务器编程的笔记
    [转] IPC之管道、FIFO、socketpair
    一些公司链接
    Delphi 通过字符串实例化类
    Delphi根据字符串实例化对象
    Class-reference types 类引用类型--快要失传的技术
    GETCLASS与REGISTERCLASS的应用一例
    Delphi XE增强的RTTI妙用--动态创建包中的窗口类
    Delphi2010的RTTI增强
  • 原文地址:https://www.cnblogs.com/pyt5208/p/556991.html
Copyright © 2011-2022 走看看