/// <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();
}
}
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
C#实现猜数字游戏
本文实例为大家分享了c#实现猜数字游戏具体代码,供大家参考,具体内容如下给定一个0-100的随机数字猜其大小题目样式:电脑产生一个0到100之间的随机数字,并且...
[阅读全文]
-
-
C# 可空类型的具体使用
在项目中我们经常会遇到可为空类型,那么到底什么是可为空类型呢?下面我们将从4个方面为大家剖析。1、可空类型基础知识顾名思义,可空类型指的就是某个对象类型可以为空...
[阅读全文]
-
-
-
C#实现猜数字小游戏
本文实例为大家分享了c#实现猜数字小游戏的具体代码,供大家参考,具体内容如下效果如图:代码:using system;using system.collecti...
[阅读全文]
-
-
C#实现简单俄罗斯方块
最近在看《.net游戏编程入门经典 c#篇》 第一章介绍了如何制作俄罗斯方块,自己试了试按照书上的步骤,可算是完成了。于是写下这篇文章留作纪念。1.类的设计在充...
[阅读全文]
-
网友评论