c#生成高清缩略图的二个示例分享
程序员文章站
2023-12-19 13:19:52
复制代码 代码如下:/// /// 为图片生成缩略图 ///
/// <summary>
/// 为图片生成缩略图
/// </summary>
/// <param name="phypath">原图片的路径</param>
/// <param name="width">缩略图宽</param>
/// <param name="height">缩略图高</param>
/// <returns></returns>
public system.drawing.image getthumbnail(system.drawing.image image, int width, intheight)
{
bitmap bmp = newbitmap(width, height);
//从bitmap创建一个system.drawing.graphics
system.drawing.graphics gr = system.drawing.graphics.fromimage(bmp);
//设置
gr.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
//下面这个也设成高质量
gr.compositingquality = system.drawing.drawing2d.compositingquality.highquality;
//下面这个设成high
gr.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
//把原始图像绘制成上面所设置宽高的缩小图
system.drawing.rectangle rectdestination = newsystem.drawing.rectangle(0, 0, width, height);
gr.drawimage(image, rectdestination, 0, 0, image.width, image.height, graphicsunit.pixel);
returnbmp;
}
httppostedfile file = photofile.postedfile;
if(!file.contenttype.contains("image"))
{
return"照片格式不合法";
}
stringext = path.getextension(file.filename).tolower();
if (ext != ".jpg" && ext != ".gif" && ext != ".png"&& ext != ".jpeg")
{
return"请您上传jpg、gif、png图片";
}
if(file.contentlength > 5 * 1024 * 1024)
{
return"请您上传512字节内的图片";
}
stringnewname = guid.newguid().tostring();
stringtemppath = "upload/";
stringimg = temppath + newname + ext;
stringfilepath = server.mappath(img);
if(!directory.exists(temppath))
{
directory.createdirectory(temppath);
}
using(system.drawing.image originalimage = system.drawing.image.fromstream(file.inputstream))
{
getthumbnail(originalimage, 504, 374).save(filepath);
public void makethumbnail(string originalimagepath, string thumbnailpath, int width, int height)
{
//获取原始图片
system.drawing.image originalimage = system.drawing.image.fromfile(originalimagepath);
//缩略图画布宽高
int towidth = width;
int toheight = height;
//原始图片写入画布坐标和宽高(用来设置裁减溢出部分)
int x = 0;
int y = 0;
int ow = originalimage.width;
int oh = originalimage.height;
//原始图片画布,设置写入缩略图画布坐标和宽高(用来原始图片整体宽高缩放)
int bg_x = 0;
int bg_y = 0;
int bg_w = towidth;
int bg_h = toheight;
//倍数变量
double multiple = 0;
//获取宽长的或是高长与缩略图的倍数
if (originalimage.width >= originalimage.height)
multiple = (double)originalimage.width / (double)width;
else
multiple = (double)originalimage.height / (double)height;
//上传的图片的宽和高小等于缩略图
if (ow <= width && oh <= height)
{
//缩略图按原始宽高
bg_w = originalimage.width;
bg_h = originalimage.height;
//空白部分用背景色填充
bg_x = convert.toint32(((double)towidth - (double)ow) / 2);
bg_y = convert.toint32(((double)toheight - (double)oh) / 2);
}
//上传的图片的宽和高大于缩略图
else
{
//宽高按比例缩放
bg_w = convert.toint32((double)originalimage.width / multiple);
bg_h = convert.toint32((double)originalimage.height / multiple);
//空白部分用背景色填充
bg_y = convert.toint32(((double)height - (double)bg_h) / 2);
bg_x = convert.toint32(((double)width - (double)bg_w) / 2);
}
//新建一个bmp图片,并设置缩略图大小.
system.drawing.image bitmap = new system.drawing.bitmap(towidth, toheight);
//新建一个画板
system.drawing.graphics g = system.drawing.graphics.fromimage(bitmap);
//设置高质量插值法
g.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybilinear;
//设置高质量,低速度呈现平滑程度
g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
//清空画布并设置背景色
g.clear(system.drawing.colortranslator.fromhtml("#f2f2f2"));
//在指定位置并且按指定大小绘制原图片的指定部分
//第一个system.drawing.rectangle是原图片的画布坐标和宽高,第二个是原图片写在画布上的坐标和宽高,最后一个参数是指定数值单位为像素
g.drawimage(originalimage, new system.drawing.rectangle(bg_x, bg_y, bg_w, bg_h), new system.drawing.rectangle(x, y, ow, oh), system.drawing.graphicsunit.pixel);
try
{
//获取图片类型
string fileextension = system.io.path.getextension(originalimagepath).tolower();
//按原图片类型保存缩略图片,不按原格式图片会出现模糊,锯齿等问题.
switch (fileextension)
{
case ".gif": bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.gif); break;
case ".jpg": bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.jpeg); break;
case ".bmp": bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.bmp); break;
case ".png": bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.png); break;
}
}
catch (system.exception e)
{
throw e;
}
finally
{
originalimage.dispose();
bitmap.dispose();
g.dispose();
}
}
复制代码 代码如下:
/// <summary>
/// 为图片生成缩略图
/// </summary>
/// <param name="phypath">原图片的路径</param>
/// <param name="width">缩略图宽</param>
/// <param name="height">缩略图高</param>
/// <returns></returns>
public system.drawing.image getthumbnail(system.drawing.image image, int width, intheight)
{
bitmap bmp = newbitmap(width, height);
//从bitmap创建一个system.drawing.graphics
system.drawing.graphics gr = system.drawing.graphics.fromimage(bmp);
//设置
gr.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
//下面这个也设成高质量
gr.compositingquality = system.drawing.drawing2d.compositingquality.highquality;
//下面这个设成high
gr.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
//把原始图像绘制成上面所设置宽高的缩小图
system.drawing.rectangle rectdestination = newsystem.drawing.rectangle(0, 0, width, height);
gr.drawimage(image, rectdestination, 0, 0, image.width, image.height, graphicsunit.pixel);
returnbmp;
}
调用方法
复制代码 代码如下:
httppostedfile file = photofile.postedfile;
if(!file.contenttype.contains("image"))
{
return"照片格式不合法";
}
stringext = path.getextension(file.filename).tolower();
if (ext != ".jpg" && ext != ".gif" && ext != ".png"&& ext != ".jpeg")
{
return"请您上传jpg、gif、png图片";
}
if(file.contentlength > 5 * 1024 * 1024)
{
return"请您上传512字节内的图片";
}
stringnewname = guid.newguid().tostring();
stringtemppath = "upload/";
stringimg = temppath + newname + ext;
stringfilepath = server.mappath(img);
if(!directory.exists(temppath))
{
directory.createdirectory(temppath);
}
using(system.drawing.image originalimage = system.drawing.image.fromstream(file.inputstream))
{
getthumbnail(originalimage, 504, 374).save(filepath);
示例2
复制代码 代码如下:
public void makethumbnail(string originalimagepath, string thumbnailpath, int width, int height)
{
//获取原始图片
system.drawing.image originalimage = system.drawing.image.fromfile(originalimagepath);
//缩略图画布宽高
int towidth = width;
int toheight = height;
//原始图片写入画布坐标和宽高(用来设置裁减溢出部分)
int x = 0;
int y = 0;
int ow = originalimage.width;
int oh = originalimage.height;
//原始图片画布,设置写入缩略图画布坐标和宽高(用来原始图片整体宽高缩放)
int bg_x = 0;
int bg_y = 0;
int bg_w = towidth;
int bg_h = toheight;
//倍数变量
double multiple = 0;
//获取宽长的或是高长与缩略图的倍数
if (originalimage.width >= originalimage.height)
multiple = (double)originalimage.width / (double)width;
else
multiple = (double)originalimage.height / (double)height;
//上传的图片的宽和高小等于缩略图
if (ow <= width && oh <= height)
{
//缩略图按原始宽高
bg_w = originalimage.width;
bg_h = originalimage.height;
//空白部分用背景色填充
bg_x = convert.toint32(((double)towidth - (double)ow) / 2);
bg_y = convert.toint32(((double)toheight - (double)oh) / 2);
}
//上传的图片的宽和高大于缩略图
else
{
//宽高按比例缩放
bg_w = convert.toint32((double)originalimage.width / multiple);
bg_h = convert.toint32((double)originalimage.height / multiple);
//空白部分用背景色填充
bg_y = convert.toint32(((double)height - (double)bg_h) / 2);
bg_x = convert.toint32(((double)width - (double)bg_w) / 2);
}
//新建一个bmp图片,并设置缩略图大小.
system.drawing.image bitmap = new system.drawing.bitmap(towidth, toheight);
//新建一个画板
system.drawing.graphics g = system.drawing.graphics.fromimage(bitmap);
//设置高质量插值法
g.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybilinear;
//设置高质量,低速度呈现平滑程度
g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
//清空画布并设置背景色
g.clear(system.drawing.colortranslator.fromhtml("#f2f2f2"));
//在指定位置并且按指定大小绘制原图片的指定部分
//第一个system.drawing.rectangle是原图片的画布坐标和宽高,第二个是原图片写在画布上的坐标和宽高,最后一个参数是指定数值单位为像素
g.drawimage(originalimage, new system.drawing.rectangle(bg_x, bg_y, bg_w, bg_h), new system.drawing.rectangle(x, y, ow, oh), system.drawing.graphicsunit.pixel);
try
{
//获取图片类型
string fileextension = system.io.path.getextension(originalimagepath).tolower();
//按原图片类型保存缩略图片,不按原格式图片会出现模糊,锯齿等问题.
switch (fileextension)
{
case ".gif": bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.gif); break;
case ".jpg": bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.jpeg); break;
case ".bmp": bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.bmp); break;
case ".png": bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.png); break;
}
}
catch (system.exception e)
{
throw e;
}
finally
{
originalimage.dispose();
bitmap.dispose();
g.dispose();
}
}