产生随机验证图片代码来自CNBlogsDottext10Beta2

本文介绍了一个使用ASP.NET创建随机验证码图片的具体实现方法。通过C#代码生成带有扭曲效果的文字验证码,并添加背景噪音,确保了验证码的难以被机器识别特性。此示例适用于网站登录验证等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

  调用方式,新建以后缀名为*.ashx的文件,然后再里面写入以下代码:

<%@ WebHandler Language="C#" Class="ViliDateImage" %>

using System;
using System.Web;
using System.Drawing.Imaging;

public class ViliDateImage : IHttpHandler {
    
    
public void ProcessRequest (HttpContext context) 
    {
        context.Response.ContentType 
= "image/jpeg";
        
using(CaptchaImage ci = new CaptchaImage(CaptchaImage.GenerateRandomCode(),156,40
))
        {
            ci.Image.Save(context.Response.OutputStream,ImageFormat.Jpeg);
        }
    }
 
    
public bool IsReusable {
        
get {
            
return false;
        }
    }

}

 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;

/// <summary>
/// 产生随机验证图片。
/// 代码来自CNBlogsDottext10Beta2。
/// http://scottwater.com/
/// http://www.cnblogs.com/
/// </summary>
public class CaptchaImage : IDisposable
{
    
// Public properties (all read-only).
    public string Text
    {
        
get { return this.text; }
    }
    
public Bitmap Image
    {
        
get { return this.image; }
    }
    
public int Width
    {
        
get { return this.width; }
    }
    
public int Height
    {
        
get { return this.height; }
    }

    
// Internal properties.
    private string text;
    
private int width;
    
private int height;
    
private string familyName = "黑体";
    
private Bitmap image;

    
// For generating random numbers.
    private Random random = new Random();

    
// ====================================================================
    
// Initializes a new instance of the CaptchaImage class using the
    
// specified text, width and height.
    
// ====================================================================
    public CaptchaImage(string s, int width, int height)
    {
        
this.text = s;
        
this.SetDimensions(width, height);
        
this.GenerateImage();
    }

    
// ====================================================================
    
// Initializes a new instance of the CaptchaImage class using the
    
// specified text, width, height and font family.
    
// ====================================================================
    public CaptchaImage(string s, int width, int height, string familyName)
    {
        
this.text = s;
        
this.SetDimensions(width, height);
        
this.SetFamilyName(familyName);
        
this.GenerateImage();
    }

    
// ====================================================================
    
// This member overrides Object.Finalize.
    
// ====================================================================
    ~CaptchaImage()
    {
        Dispose(
false);
    }

    
// ====================================================================
    
// Releases all resources used by this object.
    
// ====================================================================
    public void Dispose()
    {
        GC.SuppressFinalize(
this);
        
this.Dispose(true);
    }

    
// ====================================================================
    
// Custom Dispose method to clean up unmanaged resources.
    
// ====================================================================
    protected virtual void Dispose(bool disposing)
    {
        
if(disposing)
            
// Dispose of the bitmap.
            this.image.Dispose();
    }

    
// ====================================================================
    
// Sets the image width and height.
    
// ====================================================================
    private void SetDimensions(int width, int height)
    {
        
// Check the width and height.
        if(width <= 0)
            
throw new ArgumentOutOfRangeException("width", width, "Argument out of range, must be greater than zero.");
        
if(height <= 0)
            
throw new ArgumentOutOfRangeException("height", height, "Argument out of range, must be greater than zero.");
        
this.width = width;
        
this.height = height;
    }

    
// ====================================================================
    
// Sets the font used for the image text.
    
// ====================================================================
    private void SetFamilyName(string familyName)
    {
        
// If the named font is not installed, default to a system font.
        try
        {
            Font font 
= new Font(this.familyName, 12F);
            
this.familyName = familyName;
            font.Dispose();
        }
        
catch
        {
            
this.familyName = System.Drawing.FontFamily.GenericSerif.Name;
        }
    }

    
// ====================================================================
    
// Creates the bitmap image.
    
// ====================================================================
    private void GenerateImage()
    {
        
// Create a new 32-bit bitmap image.
        Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);

        
// Create a graphics object for drawing.
        Graphics g = Graphics.FromImage(bitmap);
        g.SmoothingMode 
= SmoothingMode.AntiAlias;
        Rectangle rect 
= new Rectangle(00this.width, this.height);

        
// Fill in the background.
        HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White);
        g.FillRectangle(hatchBrush, rect);

        
// Set up the text font.
        SizeF size;
        
float fontSize = rect.Height + 1;
        Font font;
        
// Adjust the font size until the text fits within the image.
        do
        {
            fontSize
--;
            font 
= new Font(this.familyName, fontSize, FontStyle.Bold);
            size 
= g.MeasureString(this.text, font);
        } 
while(size.Width > rect.Width);

        
// Set up the text format.
        StringFormat format = new StringFormat();
        format.Alignment 
= StringAlignment.Center;
        format.LineAlignment 
= StringAlignment.Center;

        
// Create a path using the text and warp it randomly.
        GraphicsPath path = new GraphicsPath();
        path.AddString(
this.text, font.FontFamily, (int)font.Style, font.Size, rect, format);
        
float v = 4F;
        PointF[] points 
=
            {
                
new PointF(this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
                
new PointF(rect.Width - this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
                
new PointF(this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v),
                
new PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v)
            };
        Matrix matrix 
= new Matrix();
        matrix.Translate(0F, 0F);
        path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);

        
// Draw the text.
        hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.DarkGray, Color.DarkGray);
        g.FillPath(hatchBrush, path);

        
// Add some random noise.
        int m = Math.Max(rect.Width, rect.Height);
        
for(int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
        {
            
int x = this.random.Next(rect.Width);
            
int y = this.random.Next(rect.Height);
            
int w = this.random.Next(m / 50);
            
int h = this.random.Next(m / 50);
            g.FillEllipse(hatchBrush, x, y, w, h);
        }

        
// Clean up.
        font.Dispose();
        hatchBrush.Dispose();
        g.Dispose();

        
// Set the image.
        this.image = bitmap;
    }

    
public static string GenerateRandomCode()
    {
        
string s = "";
        Random random 
= new Random();
        
for(int i = 0; i < 5; i++)
            s 
= String.Concat(s, random.Next(10).ToString());
        
return s;
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值