asp.net图片上传生成缩略图的注意事项

35 篇文章 0 订阅
25 篇文章 0 订阅

asp.net图片上传生成缩略图的注意事项

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_336_280.htm" frameborder="0" width="340" scrolling="no" height="282">

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方法,这个方法产生的缩略图质量奇差无比,不能使用!

//简单代码如下:
string path=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);
string sourceImage =sourcePath + fileName;
string thumbImage = path + fileName;

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_468_60.htm" frameborder="0" width="470" scrolling="no" height="62">

//原图(引用)
Image img=Image.FromFile(sourceImage,true);
//实际缩略图大小
System.Drawing.Size size=Pic.ImgSize(maxW,maxH,img.Width,img.Height);
int w=size.Width;
int h=size.Height;

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_ads_468_15.htm" frameborder="0" width="470" scrolling="no" height="18">

//绘制缩略图
Bitmap bitmap=new Bitmap(w,h);
Graphics g=Graphics.FromImage(bitmap);
//设定缩略图呈现质量
g.CompositingQuality=System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_468_60.htm" frameborder="0" width="470" scrolling="no" height="62">

//清洁背景
g.Clear(Color.White);
Rectangle thumbRect=new Rectangle(0,0,w,h);
g.DrawImage(img,thumbRect);
//保存缩略图
bitmap.Save(thumbImage,ImageFormat.Jpeg);
//释放内存
bitmap.Dispose();
img.Dispose();
g.Dispose();

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_syndication_468_60.htm" frameborder="0" width="470" scrolling="no" height="62">

搜索其它相关资源,请用Google搜索:
border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_search_40.htm" frameborder="0" width="460" scrolling="no" height="40">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值