ASP.NET实现上传图片并生成缩略图的方法
程序员文章站
2023-12-20 23:23:49
本文实例讲述了asp.net实现上传图片并生成缩略图的方法。分享给大家供大家参考,具体如下:
protected void bt_upload_click(obj...
本文实例讲述了asp.net实现上传图片并生成缩略图的方法。分享给大家供大家参考,具体如下:
protected void bt_upload_click(object sender, eventargs e) { //检查上传文件的格式是否有效 if (this.uploadfile.postedfile.contenttype.tolower().indexof("image") < 0) { response.write("上传图片格式无效!"); return; } //生成原图 byte[] ofilebyte = new byte[this.uploadfile.postedfile.contentlength]; system.io.stream ostream = this.uploadfile.postedfile.inputstream; system.drawing.image oimage = system.drawing.image.fromstream(ostream); int owidth = oimage.width; //原图宽度 int oheight = oimage.height; //原图高度 int twidth = 100; //设置缩略图初始宽度 int theight = 100; //设置缩略图初始高度 //按比例计算出缩略图的宽度和高度 if (owidth >= oheight) { theight = (int)math.floor(convert.todouble(oheight) * (convert.todouble(twidth) / convert.todouble(owidth))); } else { twidth = (int)math.floor(convert.todouble(owidth) * (convert.todouble(theight) / convert.todouble(oheight))); } //生成缩略原图 bitmap timage = new bitmap(twidth, theight); graphics g = graphics.fromimage(timage); g.interpolationmode = system.drawing.drawing2d.interpolationmode.high; //设置高质量插值法 g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;//设置高质量,低速度呈现平滑程度 g.clear(color.transparent); //清空画布并以透明背景色填充 g.drawimage(oimage, new rectangle(0, 0, twidth, theight), new rectangle(0, 0, owidth, oheight), graphicsunit.pixel); string ofullname = server.mappath(".") + "/image/" + "o" + datetime.now.toshortdatestring().replace("-", "") + datetime.now.hour.tostring() + datetime.now.minute.tostring() + datetime.now.second.tostring() + datetime.now.millisecond.tostring() + ".jpg"; //保存原图的物理路径 string tfullname = server.mappath(".") + "/image/" + "t" + datetime.now.toshortdatestring().replace("-", "") + datetime.now.hour.tostring() + datetime.now.minute.tostring() + datetime.now.second.tostring() + datetime.now.millisecond.tostring() + ".jpg"; //保存缩略图的物理路径 try { //以jpg格式保存图片 oimage.save(ofullname, system.drawing.imaging.imageformat.jpeg); timage.save(tfullname, system.drawing.imaging.imageformat.jpeg); } catch (exception ex) { throw ex; } finally { //释放资源 oimage.dispose(); g.dispose(); timage.dispose(); } } }
这里再补充一个改进方法:
#region 上传图片 并生成缩略图 /// <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) { //从路径中获取源图片的文件 system.drawing.image originalimage = system.drawing.image.fromfile(originalimagepath); int towidth = width; int toheight = height; int x = 0; int y = 0; //获取图片的宽度 int ow = image.width; //获取图片的高度 int oh = image.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 * toheight / towidth; x = 0; y(originalimage.height - oh) / 2; } break; default: break; } //新建一个bmp图片 bitmap bitmap = new bitmap(towidth, toheight); //新建一个画布 以bitmap 宽 高作为画布的大小 graphics g = graphics.fromimage(bitmap); //设置高质量插值法 g.interpolationmode = interpolationmode.high; //以高质量 低速度 呈现 g.smoothingmode = smoothingmode.highquality; //清空画布 以白色背景色填充 g.clear(color.transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.drawimage(originalimage,new rectangle(towidth,toheight),new rectangle(x,y,ow,oh),graphicsunit.pixel); try { //以jpg格式保存缩略图 bitmap.save(thumbnailpath,system.drawing.imaging.imageformat.jpeg); } catch (exception ex) { throw ex; } finally { //释放资源 originalimage.dispose(); bitmap.dispose(); g.dispose(); } } #endregion
希望本文所述对大家asp.net程序设计有所帮助。