生成图片式验证码试验

此博客展示了一段C#代码,用于绘制随机验证码图片。代码中创建了Bitmap对象,设置字体、随机数,将随机数绘制到图片上,最后保存图片到内存流并输出。同时还处理了可能出现的异常,给出了页面引用图片的示例。
share a simple way to generate a code for validation base on image.
 
in image.aspx.cs:
using System.Drawing;
using System.Drawing.Imaging;
private void Page_Load(object sender, System.EventArgs e)
 {
      drawimg();
 }

private void drawimg()
{
   Bitmap newBitmap = new Bitmap(36,16,PixelFormat.Format32bppArgb);
   //draw image
   Graphics g = Graphics.FromImage(newBitmap);
   //fill the area with color
   g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0,0,36,16));
   //set font object
   Font textFont = new Font("Verdana",9);
   //create RectangleF structure area
   RectangleF rectangle = new RectangleF(0,0,36,16);
   
//set a random object
   Random rd = new Random();
   
//get random number
   int valationNo = 1000 + rd.Next(8999);
   Session["valation"]=valationNo;
   //fill with color
   g.FillRectangle(new SolidBrush(Color.BurlyWood), rectangle);
   
//fill with fonts
   g.DrawString(valationNo.ToString(), textFont, new SolidBrush(Color.Blue), rectangle);
   //save the image to memory stream
   try
   {
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    newBitmap.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
    Response.ClearContent();
    Response.ContentType = "image/Jpeg";
    Response.BinaryWrite(ms.ToArray());

    //newBitmap.Save(@"C:/Inetpub/wwwroot/test0401/VaImg.gif",ImageFormat.Gif); save it to harddisk
   }
   catch(Exception ex)
   {
    Response.Write(ex.Message);
   }

}

validate-page.aspx
...
<img src="image.aspx">
...

private void drawimg()
{
   Bitmap newBitmap = new Bitmap(36,16,PixelFormat.Format32bppArgb);
   //draw image
   Graphics g = Graphics.FromImage(newBitmap);
   //fill the area with color
   g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0,0,36,16));
   //set font object
   Font textFont = new Font("Verdana",9);
   //create RectangleF structure area
   RectangleF rectangle = new RectangleF(0,0,36,16);
   
//set a random object
   Random rd = new Random();
   
//get random number
   int valationNo = 1000 + rd.Next(8999);
   Session["valation"]=valationNo;
   //fill with color
   g.FillRectangle(new SolidBrush(Color.BurlyWood), rectangle);
   
//fill with fonts
   g.DrawString(valationNo.ToString(), textFont, new SolidBrush(Color.Blue), rectangle);
   //save the image to memory stream
   try
   {
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    newBitmap.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
    Response.ClearContent();
    Response.ContentType = "image/Jpeg";
    Response.BinaryWrite(ms.ToArray());

    //newBitmap.Save(@"C:/Inetpub/wwwroot/test0401/VaImg.gif",ImageFormat.Gif); save it to harddisk
   }
   catch(Exception ex)
   {
    Response.Write(ex.Message);
   }

}

validate-page.aspx
...
<img src="image.aspx">
...

validate-page.cs
private Boolean check()
{
    if (Session["pcnum"] != null)
    {
        if (txtNumber.Text.Trim() == Session["valation"].ToString().Trim())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值