验证码

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);
        //创建字体对象
    font textfont = new font("times new roman",10);
    //创建rectanglef结构指定一个区域
    rectanglef rectangle = new rectanglef(0,0,36,16);
    //创建随机数对象
    random rd = new random();
    //取得随机数
    int valationno =rd.next();
    //使用指定的颜色填充上面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);

}

//注意,上面是我转来的,有一些字母都是小写的打头,是运行不了的.

红色部分也有误,应当是这样 :

System.Web.HttpContext.Current.Server.MapPath(

原文地址:https://www.cnblogs.com/zjw/p/1233573.html