手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >asp.net图片上传生成缩略图的注意事项
asp.net图片上传生成缩略图的注意事项
摘要:bitmap.Save(imgPath,ImageFormat.Jpeg);//这是保存缩略图的一段代码,其中的ImageFormat.Jp...

bitmap.Save(imgPath,ImageFormat.Jpeg);

//这是保存缩略图的一段代码,其中的ImageFormat.Jpeg一定不能省略,即使你保存的文件本来就是jpg格式的,也不能去掉。因为如果去掉的话,生成的缩略图比原始图片还要大!

//另外,imgPath必须首先创建,否则会产生GDI+的一般性错误。

path=System.Web.HttpContext.Current.Server.MapPath(path);

使用if(!System.IO.Directory.Exists(path))System.IO.Directiory.CreateDirectory(path);

//生成缩略图,不要使用GetThumbnailImage方法,这个方法产生的缩略图质量奇差无比,不能使用!

//简单代码如下:

stringpath=System.Web.HttpContext.Current.Server.MapPath(strpath);

sourcePath=System.Web.HttpContext.Current.Server.MapPath(sourcePath);

if(!System.IO.Directory.Exists(path))System.IO.Directory.CreateDirectory(path);

stringsourceImage=sourcePath+fileName;

stringthumbImage=path+fileName;

//原图(引用)

Imageimg=Image.FromFile(sourceImage,true);

//实际缩略图大小

System.Drawing.Sizesize=Pic.ImgSize(maxW,maxH,img.Width,img.Height);

intw=size.Width;

inth=size.Height;

//绘制缩略图

Bitmapbitmap=newBitmap(w,h);

Graphicsg=Graphics.FromImage(bitmap);

//设定缩略图呈现质量

g.CompositingQuality=System.Drawing.Drawing2D.CompositingQuality.HighQuality;

g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

//清洁背景

g.Clear(Color.White);

RectanglethumbRect=newRectangle(0,0,w,h);

g.DrawImage(img,thumbRect);

//保存缩略图

bitmap.Save(thumbImage,ImageFormat.Jpeg);

//释放内存

bitmap.Dispose();

img.Dispose();

g.Dispose();

【asp.net图片上传生成缩略图的注意事项】相关文章:

c#生成缩略图不失真的方法

asp.net中使用cookie传递参数的方法

asp.net 备份和恢复数据库的方法

asp.net下文件上传和文件删除的代码

收藏的asp.net文件上传类源码

asp.net动态载入用户控件的方法

asp.net下url传递中文的解决方案

asp.net创建位图生成验证图片类

Asp.net禁用页面缓存的方法总结

asp.net下用DataSet生成XML的问题

精品推荐
分类导航