当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net图片上传生成缩略图的注意事项

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

2018年04月25日  | 移动技术网IT编程  | 我要评论
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;

     //原图(引用)
   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;

   //绘制缩略图
   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;

   //清洁背景
   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();

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网