asp.net 生成缩略图代码
程序员文章站
2024-03-08 19:56:46
复制代码 代码如下:using system; using system.data; using system.configuration; using system.we...
复制代码 代码如下:
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;
/// <summary>
/// 图片处理类
/// 1、生成缩略图片或按照比例改变图片的大小和画质
/// 2、将生成的缩略图放到指定的目录下
/// </summary>
public class imageclass
{
public system.drawing.image resourceimage;
private int imagewidth;
private int imageheight;
public string errmessage;
/// <summary>
/// 类的构造函数
/// </summary>
/// <param name="imagefilename">图片文件的全路径名称</param>
public imageclass(string imagefilename)
{
resourceimage = system.drawing.image.fromfile(imagefilename);
errmessage = "";
}
public bool thumbnailcallback()
{
return false;
}
/// <summary>
/// 生成缩略图重载方法1,返回缩略图的image对象
/// </summary>
/// <param name="width">缩略图的宽度</param>
/// <param name="height">缩略图的高度</param>
/// <returns>缩略图的image对象</returns>
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;
}
}
/// <summary>
/// 生成缩略图重载方法2,将缩略图文件保存到指定的路径
/// </summary>
/// <param name="width">缩略图的宽度</param>
/// <param name="height">缩略图的高度</param>
/// <param name="targetfilepath">缩略图保存的全文件名,(带路径),参数格式:d:images ilename.jpg</param>
/// <returns>成功返回true,否则返回false</returns>
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;
}
}
/// <summary>
/// 生成缩略图重载方法3,返回缩略图的image对象
/// </summary>
/// <param name="percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>
/// <returns>缩略图的image对象</returns>
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;
}
}
/// <summary>
/// 生成缩略图重载方法4,返回缩略图的image对象
/// </summary>
/// <param name="percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>
/// <param name="targetfilepath">缩略图保存的全文件名,(带路径),参数格式:d:images ilename.jpg</param>
/// <returns>成功返回true,否则返回false</returns>
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;
}
}
}
上一篇: asp.net SharpZipLib的压缩与解压问题
下一篇: asp.net中简体转繁体实现代码