使用c#生成gif动画验证码及给gif动画加上水印代码
2010-07-17 14:42
746 查看
using System;
using System.IO;
using System.Web;
using System.Drawing;
//GIF验证码类
public class Validate
{
//设置最少4位验证码
private byte TrueValidateCodeCount = 4;
public byte ValidateCodeCount
{
get
{
return TrueValidateCodeCount;
}
set
{
//验证码至少为3位
if (value > 4)
TrueValidateCodeCount = value;
}
}
protected string ValidateCode = "";
//是否消除锯齿
public bool FontTextRenderingHint = false;
//验证码字体
public string ValidateCodeFont = "Arial";
//验证码型号(像素)
public float ValidateCodeSize = 13;
public int ImageHeight = 23;
//定义验证码中所有的字符
public string AllChar = "1,2,3,4,5,6,7,8,9,0,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z"; //获得随机四位数
private void CreateValidate()
{
ValidateCode = "";
//将验证码中所有的字符保存在一个字符串数组中
string[] CharArray = AllChar.Split(',');
int Temp = -1;
//生成一个随机对象
Random RandCode = new Random();
//根据验证码的位数循环
for (int i = 0; i < ValidateCodeCount; i++)
{
//主要是防止生成相同的验证码
if (Temp != -1)
{
//加入时间的刻度
RandCode = new Random(i * Temp * ((int)DateTime.Now.Ticks));
}
int t = RandCode.Next(35);
if (Temp == t)
{
//相等的话重新生成
CreateValidate();
}
Temp = t;
ValidateCode += CharArray[Temp];
}
//错误检测,去除超过指定位数的验证码
if (ValidateCode.Length > TrueValidateCodeCount)
ValidateCode = ValidateCode.Remove(TrueValidateCodeCount);
}
//生成一帧的BMP图象
private void CreateImageBmp(out Bitmap ImageFrame)
{
//获得验证码字符
char[] CodeCharArray = ValidateCode.ToCharArray(0, ValidateCodeCount);
//图像的宽度-与验证码的长度成一定比例
int ImageWidth = (int)(TrueValidateCodeCount * ValidateCodeSize * 1.3 + 4);
//创建一个长20,宽iwidth的图像对象
ImageFrame = new Bitmap(ImageWidth, ImageHeight);
//创建一个新绘图对象
Graphics ImageGraphics = Graphics.FromImage(ImageFrame);
//清除背景色,并填充背景色
//Note:Color.Transparent为透明
ImageGraphics.Clear(Color.White);
//绘图用的字体和字号
Font CodeFont = new Font(ValidateCodeFont, ValidateCodeSize, FontStyle.Bold);
//绘图用的刷子大小
Brush ImageBrush = new SolidBrush(Color.Red);
//字体高度计算
int FontHeight = (int)Math.Max(ImageHeight - ValidateCodeSize - 3, 2);
//创建随机对象
Random rand = new Random();
//开始随机安排字符的位置,并画到图像里
for (int i = 0; i < TrueValidateCodeCount; i++)
{
//生成随机点,决定字符串的开始输出范围
int[] FontCoordinate = new int[2];
FontCoordinate[0] = (int)(i * ValidateCodeSize + rand.Next(1)) + 3;
FontCoordinate[1] = rand.Next(FontHeight);
Point FontDrawPoint = new Point(FontCoordinate[0], FontCoordinate[1]);
//消除锯齿操作
if (FontTextRenderingHint)
ImageGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
else
ImageGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
//格式化刷子属性-用指定的刷子、颜色等在指定的范围内画图
ImageGraphics.DrawString(CodeCharArray[i].ToString(), CodeFont, ImageBrush, FontDrawPoint);
}
ImageGraphics.Dispose();
}
//处理生成的BMP
private void DisposeImageBmp(ref Bitmap ImageFrame)
{
//创建绘图对象
Graphics ImageGraphics = Graphics.FromImage(ImageFrame);
//创建铅笔对象
Pen ImagePen = new Pen(Color.Red, 1);
//创建随机对象
Random rand = new Random();
//创建随机点
Point[] RandPoint = new Point[2];
//随机画线
for (int i = 0; i < 15; i++)
{
RandPoint[0] = new Point(rand.Next(ImageFrame.Width), rand.Next(ImageFrame.Height));
RandPoint[1] = new Point(rand.Next(ImageFrame.Width), rand.Next(ImageFrame.Height));
ImageGraphics.DrawLine(ImagePen, RandPoint[0], RandPoint[1]);
}
ImageGraphics.Dispose();
}
//创建GIF动画
private void CreateImageGif()
{
Bitmap ImageFrame;
Kissogram.Drawing.Gif.AnimatedGifEncoder GifPic = new Kissogram.Drawing.Gif.AnimatedGifEncoder();
MemoryStream BmpMemory = new MemoryStream();
GifPic.Start();
//确保视觉残留
GifPic.SetDelay(5);
//-1:no repeat,0:always repeat
GifPic.SetRepeat(0);
for (int i = 0; i < 20; i++)
{
//创建一帧的图像
CreateImageBmp(out ImageFrame);
//生成随机线条
DisposeImageBmp(ref ImageFrame);
//输出绘图,将图像保存到指定的流
ImageFrame.Save(BmpMemory, System.Drawing.Imaging.ImageFormat.Png);
GifPic.AddFrame(Image.FromStream(BmpMemory));
BmpMemory = new MemoryStream();
}
GifPic.OutPut(ref BmpMemory);
HttpContext.Current.Response.ClearContent();
//配置输出类型
HttpContext.Current.Response.ContentType = "image/Gif";
//输出内容
HttpContext.Current.Response.BinaryWrite(BmpMemory.ToArray());
BmpMemory.Close();
BmpMemory.Dispose();
}
//输出验证码
public void OutPutValidate(string ValidateCodeSession)
{
CreateValidate();
CreateImageGif();
//把生成的验证码输入到SESSION
HttpContext.Current.Session[ValidateCodeSession] = ValidateCode;
}
} //加水印的代码: public static Bitmap WaterMarkWithText(System.Drawing.Bitmap origialGif, string text,string filePath)
{
//用于存放桢
List<Frame> frames = new List<Frame>();
//如果不是gif文件,直接返回原图像
if (origialGif.RawFormat.Guid != System.Drawing.Imaging.ImageFormat.Gif.Guid)
{
return origialGif;
}
//如果该图像是gif文件
foreach (Guid guid in origialGif.FrameDimensionsList)
{
System.Drawing.Imaging.FrameDimension frameDimension = new System.Drawing.Imaging.FrameDimension(guid);
int frameCount = origialGif.GetFrameCount(frameDimension);
for (int i = 0; i < frameCount; i++)
{
if (origialGif.SelectActiveFrame(frameDimension, i) == 0)
{
int delay = Convert.ToInt32(origialGif.GetPropertyItem(20736).Value.GetValue(i));
Image img = Image.FromHbitmap(origialGif.GetHbitmap());
Font font = new Font(new FontFamily("宋体"), 35.0f,FontStyle.Bold);
Graphics g = Graphics.FromImage(img);
g.DrawString(text, font, Brushes.BlanchedAlmond, new PointF(10.0f, 10.0f));
Frame frame = new Frame(img, delay);
frames.Add(frame);
}
}
Gif.Components.AnimatedGifEncoder gif = new Gif.Components.AnimatedGifEncoder();
gif.Start(filePath);
gif.SetDelay(100);
gif.SetRepeat(0);
for (int i = 0; i < frames.Count; i++)
{
gif.AddFrame(frames[i].Image);
}
gif.Finish();
try
{
Bitmap gifImg = (Bitmap)Bitmap.FromFile(filePath);
return gifImg;
}
catch
{
return origialGif;
}
}
return origialGif;
}
using System.IO;
using System.Web;
using System.Drawing;
//GIF验证码类
public class Validate
{
//设置最少4位验证码
private byte TrueValidateCodeCount = 4;
public byte ValidateCodeCount
{
get
{
return TrueValidateCodeCount;
}
set
{
//验证码至少为3位
if (value > 4)
TrueValidateCodeCount = value;
}
}
protected string ValidateCode = "";
//是否消除锯齿
public bool FontTextRenderingHint = false;
//验证码字体
public string ValidateCodeFont = "Arial";
//验证码型号(像素)
public float ValidateCodeSize = 13;
public int ImageHeight = 23;
//定义验证码中所有的字符
public string AllChar = "1,2,3,4,5,6,7,8,9,0,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z"; //获得随机四位数
private void CreateValidate()
{
ValidateCode = "";
//将验证码中所有的字符保存在一个字符串数组中
string[] CharArray = AllChar.Split(',');
int Temp = -1;
//生成一个随机对象
Random RandCode = new Random();
//根据验证码的位数循环
for (int i = 0; i < ValidateCodeCount; i++)
{
//主要是防止生成相同的验证码
if (Temp != -1)
{
//加入时间的刻度
RandCode = new Random(i * Temp * ((int)DateTime.Now.Ticks));
}
int t = RandCode.Next(35);
if (Temp == t)
{
//相等的话重新生成
CreateValidate();
}
Temp = t;
ValidateCode += CharArray[Temp];
}
//错误检测,去除超过指定位数的验证码
if (ValidateCode.Length > TrueValidateCodeCount)
ValidateCode = ValidateCode.Remove(TrueValidateCodeCount);
}
//生成一帧的BMP图象
private void CreateImageBmp(out Bitmap ImageFrame)
{
//获得验证码字符
char[] CodeCharArray = ValidateCode.ToCharArray(0, ValidateCodeCount);
//图像的宽度-与验证码的长度成一定比例
int ImageWidth = (int)(TrueValidateCodeCount * ValidateCodeSize * 1.3 + 4);
//创建一个长20,宽iwidth的图像对象
ImageFrame = new Bitmap(ImageWidth, ImageHeight);
//创建一个新绘图对象
Graphics ImageGraphics = Graphics.FromImage(ImageFrame);
//清除背景色,并填充背景色
//Note:Color.Transparent为透明
ImageGraphics.Clear(Color.White);
//绘图用的字体和字号
Font CodeFont = new Font(ValidateCodeFont, ValidateCodeSize, FontStyle.Bold);
//绘图用的刷子大小
Brush ImageBrush = new SolidBrush(Color.Red);
//字体高度计算
int FontHeight = (int)Math.Max(ImageHeight - ValidateCodeSize - 3, 2);
//创建随机对象
Random rand = new Random();
//开始随机安排字符的位置,并画到图像里
for (int i = 0; i < TrueValidateCodeCount; i++)
{
//生成随机点,决定字符串的开始输出范围
int[] FontCoordinate = new int[2];
FontCoordinate[0] = (int)(i * ValidateCodeSize + rand.Next(1)) + 3;
FontCoordinate[1] = rand.Next(FontHeight);
Point FontDrawPoint = new Point(FontCoordinate[0], FontCoordinate[1]);
//消除锯齿操作
if (FontTextRenderingHint)
ImageGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
else
ImageGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
//格式化刷子属性-用指定的刷子、颜色等在指定的范围内画图
ImageGraphics.DrawString(CodeCharArray[i].ToString(), CodeFont, ImageBrush, FontDrawPoint);
}
ImageGraphics.Dispose();
}
//处理生成的BMP
private void DisposeImageBmp(ref Bitmap ImageFrame)
{
//创建绘图对象
Graphics ImageGraphics = Graphics.FromImage(ImageFrame);
//创建铅笔对象
Pen ImagePen = new Pen(Color.Red, 1);
//创建随机对象
Random rand = new Random();
//创建随机点
Point[] RandPoint = new Point[2];
//随机画线
for (int i = 0; i < 15; i++)
{
RandPoint[0] = new Point(rand.Next(ImageFrame.Width), rand.Next(ImageFrame.Height));
RandPoint[1] = new Point(rand.Next(ImageFrame.Width), rand.Next(ImageFrame.Height));
ImageGraphics.DrawLine(ImagePen, RandPoint[0], RandPoint[1]);
}
ImageGraphics.Dispose();
}
//创建GIF动画
private void CreateImageGif()
{
Bitmap ImageFrame;
Kissogram.Drawing.Gif.AnimatedGifEncoder GifPic = new Kissogram.Drawing.Gif.AnimatedGifEncoder();
MemoryStream BmpMemory = new MemoryStream();
GifPic.Start();
//确保视觉残留
GifPic.SetDelay(5);
//-1:no repeat,0:always repeat
GifPic.SetRepeat(0);
for (int i = 0; i < 20; i++)
{
//创建一帧的图像
CreateImageBmp(out ImageFrame);
//生成随机线条
DisposeImageBmp(ref ImageFrame);
//输出绘图,将图像保存到指定的流
ImageFrame.Save(BmpMemory, System.Drawing.Imaging.ImageFormat.Png);
GifPic.AddFrame(Image.FromStream(BmpMemory));
BmpMemory = new MemoryStream();
}
GifPic.OutPut(ref BmpMemory);
HttpContext.Current.Response.ClearContent();
//配置输出类型
HttpContext.Current.Response.ContentType = "image/Gif";
//输出内容
HttpContext.Current.Response.BinaryWrite(BmpMemory.ToArray());
BmpMemory.Close();
BmpMemory.Dispose();
}
//输出验证码
public void OutPutValidate(string ValidateCodeSession)
{
CreateValidate();
CreateImageGif();
//把生成的验证码输入到SESSION
HttpContext.Current.Session[ValidateCodeSession] = ValidateCode;
}
} //加水印的代码: public static Bitmap WaterMarkWithText(System.Drawing.Bitmap origialGif, string text,string filePath)
{
//用于存放桢
List<Frame> frames = new List<Frame>();
//如果不是gif文件,直接返回原图像
if (origialGif.RawFormat.Guid != System.Drawing.Imaging.ImageFormat.Gif.Guid)
{
return origialGif;
}
//如果该图像是gif文件
foreach (Guid guid in origialGif.FrameDimensionsList)
{
System.Drawing.Imaging.FrameDimension frameDimension = new System.Drawing.Imaging.FrameDimension(guid);
int frameCount = origialGif.GetFrameCount(frameDimension);
for (int i = 0; i < frameCount; i++)
{
if (origialGif.SelectActiveFrame(frameDimension, i) == 0)
{
int delay = Convert.ToInt32(origialGif.GetPropertyItem(20736).Value.GetValue(i));
Image img = Image.FromHbitmap(origialGif.GetHbitmap());
Font font = new Font(new FontFamily("宋体"), 35.0f,FontStyle.Bold);
Graphics g = Graphics.FromImage(img);
g.DrawString(text, font, Brushes.BlanchedAlmond, new PointF(10.0f, 10.0f));
Frame frame = new Frame(img, delay);
frames.Add(frame);
}
}
Gif.Components.AnimatedGifEncoder gif = new Gif.Components.AnimatedGifEncoder();
gif.Start(filePath);
gif.SetDelay(100);
gif.SetRepeat(0);
for (int i = 0; i < frames.Count; i++)
{
gif.AddFrame(frames[i].Image);
}
gif.Finish();
try
{
Bitmap gifImg = (Bitmap)Bitmap.FromFile(filePath);
return gifImg;
}
catch
{
return origialGif;
}
}
return origialGif;
}
相关文章推荐
- 【代码】PHP 生成GIF动画实现动态图片验证码
- .net c# gif动画如何添加图片水印实现思路及代码
- .net c# gif动画如何添加图片水印实现思路及代码学习
- .net c# gif动画如何添加图片水印实现思路及代码
- asp.net(C#) 生成随机验证码的代码
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- PHP使用GIFEncoder类生成的GIF动态图片验证码
- C#一个gif动画的验证码
- 單純使用PHP腳本不借助任何外部库或图片以及字体产生动画效果的Gif验证码图片
- AutoCode (vs插件)自动生成代码插件 下载及使用教程(C#版)
- C#分析数据库结构,使用XSL模板自动生成代码(二)
- C#分析数据库结构,使用XSL模板自动生成代码 - 清清月儿 .NET万花筒 Asp.net技术 Asp.net教程 Asp.net源码 Asp.net基础 Asp.net控件 Asp.net入门 - CSDNBlog
- C#分析数据库结构,使用XSL模板自动生成代码
- php生成GIF动态验证码图片(代码家园)
- java生成动态gif格式与png格式的验证码(代码2)
- C#新手入门代码 VS2015使用技巧 生成类图的步骤
- C#分析数据库结构,使用XSL模板自动生成代码
- [导入]C#分析数据库结构,使用XSL模板自动生成代码
- PHP 使用 Imagick 裁切/生成缩略图/添加水印自动检测和处理 GIF
- asp.net(c#) 使用Rex正则来生成字符串数组的代码