当前位置: 移动技术网 > IT编程>开发语言>.net > c#生成图片缩略图的类(2种实现思路)

c#生成图片缩略图的类(2种实现思路)

2017年12月12日  | 移动技术网IT编程  | 我要评论
第一种 复制代码 代码如下: /**//// <summary> /// 生成缩略图 /// </summary> /// <param na
第一种
复制代码 代码如下:

/**//// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalimagepath">源图路径(物理路径)</param>
/// <param name="thumbnailpath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void makethumbnail(string originalimagepath, string thumbnailpath, int width, int height, string mode)
{
image originalimage = image.fromfile(originalimagepath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalimage.width;
int oh = originalimage.height;
switch (mode)
{
case "hw"://指定高宽缩放(可能变形)
break;
case "w"://指定宽,高按比例
toheight = originalimage.height * width/originalimage.width;
break;
case "h"://指定高,宽按比例
towidth = originalimage.width * height/originalimage.height;
break;
case "cut"://指定高宽裁减(不变形)
if((double)originalimage.width/(double)originalimage.height > (double)towidth/(double)toheight)
{
oh = originalimage.height;
ow = originalimage.height*towidth/toheight;
y = 0;
x = (originalimage.width - ow)/2;
}
else
{
ow = originalimage.width;
oh = originalimage.width*height/towidth;
x = 0;
y = (originalimage.height - oh)/2;
}
break;
default :
break;
}
//新建一个bmp图片
image bitmap = new system.drawing.bitmap(towidth,toheight);
//新建一个画板
graphics g = system.drawing.graphics.fromimage(bitmap);
//设置高质量插值法
g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
//设置高质量,低速度呈现平滑程度
g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
//清空画布并以透明背景色填充
g.clear(color.transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.drawimage(originalimage, new rectangle(0, 0, towidth, toheight),
new rectangle(x, y, ow,oh),
graphicsunit.pixel);
try
{
//以jpg格式保存缩略图
bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.jpeg);
}
catch(system.exception e)
{
throw e;
}
finally
{
originalimage.dispose();
bitmap.dispose();
g.dispose();
}
}

关键方法graphics.drawimage见ms-help://ms.netframeworksdkv1.1.chs/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11.htm
第二种
4个重载方法,有直接返回image对象的,有生成缩略图,并且保存到指定目录的!
复制代码 代码如下:

using system.io;
using system.drawing;
using system.drawing.imaging;
/// <summary>
/// 图片处理类
/// 1、生成缩略图片或按照比例改变图片的大小和画质
/// 2、将生成的缩略图放到指定的目录下
/// </summary>
public class imageclass
{
public image resourceimage;
private int imagewidth;
private int imageheight;
public string errmessage;
/// <summary>
/// 类的构造函数
/// </summary>
/// <param name="imagefilename">图片文件的全路径名称</param>
public imageclass(string imagefilename)
{
resourceimage=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 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;
}
}
/// <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
{
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;
}
}
/// <summary>
/// 生成缩略图重载方法3,返回缩略图的image对象
/// </summary>
/// <param name="percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>
/// <returns>缩略图的image对象</returns>
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;
}
}
/// <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
{
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;
}
}
}

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网