using system;
using system.data;
using system.configuration;
using system.web;
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.io;
using system.drawing;
using system.drawing.imaging;
///
/// 图片处理类
/// 1、生成缩略图片或按照比例改变图片的大小和画质
/// 2、将生成的缩略图放到指定的目录下
///
public class imageclass
{
public system.drawing.image resourceimage;
private int imagewidth;
private int imageheight;
public string errmessage;
///
/// 类的构造函数
///
/// 图片文件的全路径名称
public imageclass(string imagefilename)
{
resourceimage = system.drawing.image.fromfile(imagefilename);
errmessage = ;
}
public bool thumbnailcallback()
{
return false;
}
///
/// 生成缩略图重载方法1,返回缩略图的image对象
///
/// 缩略图的宽度
/// 缩略图的高度
/// 缩略图的image对象
public system.drawing.image getreducedimage(int width, int height)
{
try
{
system.drawing.image reducedimage;
system.drawing.image.getthumbnailimageabort callb = new system.drawing.image.getthumbnailimageabort(thumbnailcallback);
reducedimage = resourceimage.getthumbnailimage(width, height, callb, intptr.zero);
return reducedimage;
}
catch (exception e)
{
errmessage = e.message;
return null;
}
}
///
/// 生成缩略图重载方法2,将缩略图文件保存到指定的路径
///
/// 缩略图的宽度
/// 缩略图的高度
/// 缩略图保存的全文件名,(带路径),参数格式:d:images ilename.jpg
/// 成功返回true,否则返回false
public bool getreducedimage(int width, int height, string targetfilepath)
{
try
{
system.drawing.image reducedimage;
system.drawing.image.getthumbnailimageabort callb = new system.drawing.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 system.drawing.image getreducedimage(double percent)
{
try
{
system.drawing.image reducedimage;
system.drawing.image.getthumbnailimageabort callb = new system.drawing.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 ilename.jpg
/// 成功返回true,否则返回false
public bool getreducedimage(double percent, string targetfilepath)
{
try
{
system.drawing.image reducedimage;
system.drawing.image.getthumbnailimageabort callb = new system.drawing.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;
}
}
}
更多信息请查看IT技术专栏