生成验证码
来源:互联网 发布:ubuntu likewise open 编辑:程序博客网 时间:2024/06/06 17:54
先生成随机的四位字母,每个字母在生成的图像中尺寸随机生成,有一定角度的旋转,颜色也有一定程度的变化
var code = Request["code"];
if (code == null)throw new HttpException(403, "need code");
Response.ContentType = "image/jpeg";
Response.BufferOutput = true;
Response.Clear();
using (var image = new Bitmap(640, 320))
{
image.SetResolution(72.0f, 72.0f);
using (var graphics = Graphics.FromImage(image))
{
WebColorConverter ww = new WebColorConverter();
graphics.Clear((Color)ww.ConvertFromString("#FAE264"));
Random random = new Random();
//画图片的背景噪音线
for (int i = 0; i < 12; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
graphics.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
}
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
{
using (var font = new Font("Arial", (float)(random.NextDouble()*100+100), FontStyle.Bold | FontStyle.Italic))
{
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Red, (float)(random.NextDouble()*20+30), true);
System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
matrix.Rotate((float)(random.NextDouble()*20-10));
graphics.Transform = matrix;
graphics.DrawString(random.Next(0, 2) == 0 ? code.Substring(0, 1).ToUpper() : code.Substring(0, 1), font, brush, random.Next(30, 40), random.Next(35, 45));
}
using (var font = new Font("Arial", (float)(random.NextDouble() * 100 + 100), FontStyle.Bold | FontStyle.Italic))
{
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.Purple, Color.Pink, (float)(random.NextDouble() * 20 + 30), true);
System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
matrix.Rotate((float)(random.NextDouble() * 20 - 10));
graphics.Transform = matrix;
graphics.DrawString(random.Next(0, 2) == 0 ? code.Substring(1, 1).ToUpper() : code.Substring(1, 1), font, brush, random.Next(172, 183), random.Next(35, 45));
}
using (var font = new Font("Arial", (float)(random.NextDouble() * 100 + 100), FontStyle.Bold | FontStyle.Italic))
{
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.White, (float)(random.NextDouble() * 20 + 30), true);
System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
matrix.Rotate((float)(random.NextDouble() * 20 - 10));
graphics.Transform = matrix;
graphics.DrawString(random.Next(0, 2) == 0 ? code.Substring(2, 1).ToUpper() : code.Substring(2, 1), font, brush, random.Next(314, 324), random.Next(35, 45));
}
using (var font = new Font("Arial", (float)(random.NextDouble() * 100 + 100), FontStyle.Bold | FontStyle.Italic))
{
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.Silver, Color.Red, (float)(random.NextDouble() * 20 + 30), true);
System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
matrix.Rotate((float)(random.NextDouble() * 20 - 10));
graphics.Transform = matrix;
graphics.DrawString(random.Next(0, 2) == 0 ? code.Substring(3, 1).ToUpper() : code.Substring(3, 1), font, brush, random.Next(456, 466), random.Next(35, 45));
}
}
}
var jpegParams = new EncoderParameters(1);
jpegParams.Param[0] = new EncoderParameter(Encoder.Quality, 30L);
image.Save(Response.OutputStream, ImageCodecInfo.GetImageDecoders().Single(decoder => decoder.FormatID == ImageFormat.Jpeg.Guid), jpegParams);
Response.Flush();
- 验证码生成及验证
- 生成验证码并验证
- 验证码生成与验证
- C#生成验证码
- C#生成验证码
- 生成验证码
- 验证码生成
- 生成验证码
- JSP生成验证码
- 生成验证码
- 生成验证码
- 数字验证码生成
- 动态生成验证码
- 生成验证码(1)
- 生成验证码(2)
- c# 生成验证码
- 生成验证码图像
- 生成验证码
- 设计模式之访问者模式
- Spring声明式事务配置管理方法
- 3 磁盘与文件系统管理
- 使用Memory Analyzer tool(MAT)分析内存泄漏
- linux线程之间的通信
- 生成验证码
- Java虚拟机(JVM)中的内存设置详解
- 在interceptor中获得namespace和actionname
- nginx 设置403、404、等错误页面
- iptables出错18446744073709551615和2.6.23以后使用connlimit的方法
- Windows下的Objective-C集成开发环境(IDE)的搭建
- spring mvc 流程图
- 寻找容器vector中出现最多的元素以及出现的次数
- 时间的思考---《把时间当作朋友》读后感