里面有4个重载方法,有直接返回Image对象的,有生成缩略图,并且保存到指定目录的!
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
///
/// 图片处理类
/// 1、生成缩略图片或按照比例改变图片的大小和画质
/// 2、将生成的缩略图放到指定的目录下
///
public class ImageClass
{
public Image ResourceImage;
private int ImageWidth;
private int ImageHeight;
public string ErrMessage;
///
/// 类的构造函数
///
/// 图片文件的全路径名称
public ImageClass(string ImageFileName)
{
ResourceImage=Image.FromFile(ImageFileName);
ErrMessage="";
}
public bool ThumbnailCallback()
{
return false;
}
///
/// 生成缩略图重载方法1,返回缩略图的Image对象
///
/// 缩略图的宽度
/// 缩略图的高度
/// 缩略图的Image对象
public Image GetReducedImage(int Width,int Height)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);
ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero);
return ReducedImage;
}
catch(Exception e)
{
ErrMessage=e.Message;
return null;
}
}
///
/// 生成缩略图重载方法2,将缩略图文件保存到指定的路径
///
/// 缩略图的宽度
/// 缩略图的高度
/// 缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg
/// 成功返回true,否则返回false
public bool GetReducedImage(int Width,int Height,string targetFilePath)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);
ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero);
ReducedImage.Save(@targetFilePath,ImageFormat.Jpeg);
ReducedImage.Dispose();
return true;
}
catch(Exception e)
{
ErrMessage=e.Message;
return false;
}
}
///
/// 生成缩略图重载方法3,返回缩略图的Image对象
///
/// 缩略图的宽度百分比 如:需要百分之80,就填0.8
/// 缩略图的Image对象
public Image GetReducedImage(double Percent)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth=Convert.ToInt32(ResourceImage.Width*Percent);
ImageHeight=Convert.ToInt32(ResourceImage.Width*Percent);
ReducedImage=ResourceImage.GetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtr.Zero);
return ReducedImage;
}
catch(Exception e)
{
ErrMessage=e.Message;
return null;
}
}
///
/// 生成缩略图重载方法4,返回缩略图的Image对象
///
/// 缩略图的宽度百分比 如:需要百分之80,就填0.8
/// 缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg
/// 成功返回true,否则返回false
public bool GetReducedImage(double Percent,string targetFilePath)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth=Convert.ToInt32(ResourceImage.Width*Percent);
ImageHeight=Convert.ToInt32(ResourceImage.Width*Percent);
ReducedImage=ResourceImage.GetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtr.Zero);
ReducedImage.Save(@targetFilePath,ImageFormat.Jpeg);
ReducedImage.Dispose();
return true;
}
catch(Exception e)
{
ErrMessage=e.Message;
return false;
}
}
}
using System;
using System.IO;
using System.Web;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
/// <summary>
///Cls_PicVcode生成图片
/// </summary>
public class ValidateCode
{
/// <summary>
/// 该方法用于生成指定位数的随机数
/// </summary>
/// <param name="VcodeNum">参数是随机数的位数</param>
/// <returns>返回一个随机数字符串</returns>
private string RndNum(int VcodeNum)
{
string Vchar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P" +
",Q,R,S,T,U,V,W,X,Y,Z";
string[] VcArray = Vchar.Split(new Char[] { '','' });//拆分成数组
string VNum = "";//产生的随机数
int temp = -1;//记录上次随机数值,尽量避免生产几个一样的随机数
Random rand = new Random();
for (int i = 1; i < VcodeNum + 1; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(33);
if (temp != -1 && temp == t)
{
return RndNum(VcodeNum);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}
/// <summary>
/// 该方法是将生成的随机数写入图像文件
/// </summary>
/// <param name="VNum">VNum是一个随机数</param>
public MemoryStream Create(out string VNum)
{
VNum = RndNum(4);
Bitmap Img = null;
Graphics g = null;
MemoryStream ms = null;
System.Random random = new Random();
//验证码颜色集合
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
//验证码字体集合
string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
//定义图像的大小,生成图像的实例
Img = new Bitmap(100, 25);
g = Graphics.FromImage(Img);//从Img对象生成新的Graphics对象
g.Clear(Color.White);//背景设为白色
//在随机位置画背景点
for (int i = 0; i < 100; i++)
{
int x = random.Next(Img.Width);
int y = random.Next(Img.Height);
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
}
//验证码绘制在g中
for (int i = 0; i < VNum.Length; i++)
{
int cindex = random.Next(7);//随机颜色索引值
int findex = random.Next(5);//随机字体索引值
Font f = new System.Drawing.Font(fonts[findex], 14, System.Drawing.FontStyle.Bold);//字体
Brush b = new System.Drawing.SolidBrush(c[cindex]);//颜色
int ii = 4;
if ((i + 1) % 2 == 0)//控制验证码不在同一高度
{
ii = 2;
}
g.DrawString(VNum.Substring(i, 1), f, b, 3 + (i * 20), ii);//绘制一个验证字符
}
ms = new MemoryStream();//生成内存流对象
Img.Save(ms, ImageFormat.Jpeg);//将此图像以jpg图像文件的格式保存到流中
//回收资源
g.Dispose();
Img.Dispose();
return ms;
}
}