随机生成图片
来源:互联网 发布:永久域名多少钱 编辑:程序博客网 时间:2024/05/17 18:04
<asp:Image ID="imgAntiBotImage" runat="server" ImageUrl="~/antibotimage.ashx" EnableViewState="false" />
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//得到新的验证码
Session["antibotimage"] = PublicMethod.generateRandomString(4).ToUpper();
}
}
/// <summary>
/// 得到验证码(管理员登录)
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
public static string generateRandomString(int size)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString();
}
[用ashx写的]
<%@ WebHandler Language="C#" Class="AntiBotImage" %>
//===============================================================================================
//
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
//
//===============================================================================================
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
/// <summary>
/// This Handler renders out a so called Anti-Bot image. It is used to prevent bots from making automatic entries in the guestbook.
/// The random text for the image is taken out of the session-variable "antibotimage". If this value is not present in the session, a empty gif is returned.
/// </summary>
public class AntiBotImage : IHttpHandler, IRequiresSessionState
{
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
if (context.Session["antibotimage"] == null)
{
context.Session["antibotimage"] = generateRandomString(4).ToUpper();
}
GenerateImage(context.Session["antibotimage"].ToString(), 112, 27, "Arial").Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
#endregion
private Bitmap GenerateImage(string text, int width, int height, string fontFamily)
{
Random random = new Random();
// Create a new 32-bit bitmap image.
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
// Create a graphics object for drawing.
Graphics g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rect = new Rectangle(0, 0, width, height);
// Fill in the background.
//HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White);
HatchBrush hatchBrush = new HatchBrush(HatchStyle.Wave, Color.LightGray, Color.White);
g.FillRectangle(hatchBrush, rect);
// Set up the text font.
SizeF size;
float fontSize = rect.Height + 1;
Font font;
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
// Adjust the font size until the text fits within the image.
do
{
fontSize--;
font = new Font(fontFamily, fontSize, FontStyle.Bold);
size = g.MeasureString(text, font, new SizeF(width, height), format);
} while (size.Width > rect.Width);
// Create a path using the text and warp it randomly.
GraphicsPath path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, font.Size, rect, format);
float v = 4F;
PointF[] points =
{
new PointF(random.Next(rect.Width) / v, random.Next(rect.Height) / v),
new PointF(rect.Width - random.Next(rect.Width) / v, random.Next(rect.Height) / v),
new PointF(random.Next(rect.Width) / v, rect.Height - random.Next(rect.Height) / v),
new PointF(rect.Width - random.Next(rect.Width) / v, rect.Height - 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.LightGray, Color.DarkGray);
hatchBrush = new HatchBrush(HatchStyle.DashedUpwardDiagonal, Color.DarkGray, Color.Black);
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 = random.Next(rect.Width);
int y = random.Next(rect.Height);
int w = random.Next(m / 50);
int h = random.Next(m / 50);
g.FillEllipse(hatchBrush, x, y, w, h);
}
// Clean up.
font.Dispose();
hatchBrush.Dispose();
g.Dispose();
return bitmap;
}
private string generateRandomString(int size)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString();
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/mawei_0210/archive/2009/08/20/4465542.aspx
- 随机生成图片
- 随机生成图片
- 随机生成图片
- Servlet生成随机图片
- 随机生成汉字图片
- 随机生成图片验证码
- 随机生成图片验证码
- 随机生成图片验证码
- 生成随机验证码图片
- C#生成随机验证图片
- 生成随机图片验证码
- 随机生成图片验证码
- 随机生成图片验证码
- 生成随机图片验证码
- 随机生成图片验证码
- Python-生成随机码图片
- 生成随机验证码图片
- Servlet随机生成验证图片
- 独立任务最优调度
- 心跳防护程序 收藏
- log4j.properties配置
- flex 处理java后台返回的JavaBean
- 实习 真的能变强吗???
- 随机生成图片
- 用Javascript定位元素位置全攻略
- 模拟指定大小的堆栈溢出、移除操作 !
- open()与fopen()函数
- 链表的结点插入,删除等操作
- Spring中数据源的配置详解
- 开源编辑器Notepad++
- 什么是WMI?
- ALE IDOC配置步骤