如题,我现在已经可以生成缩略图并且存入数据库中,可是缩略图很模糊,请高手赐教!
不要问存入数据库有什么好处,主要是安全性好,易管理,不生成垃圾文件,以后换程序好换,数据 不容易丢失,容易备份!只是个人这么认为,我有点钻牛角尖的感觉!呵呵。

解决方案 »

  1.   

    记得有个CMS里包含了一个老外写的缩略图生成插件,不错的,可惜不记得是哪个CMS了
    动易?科讯?NBArticle?
    下面这个里面写的看能不能满足你的要求
    http://www.freevip.cn/view.asp?id=689
      

  2.   

    using System;
    namespace vote
    {
     /// <summary>
     /// UploadFile 的摘要说明。
     /// </summary>
     using System; 
     using System.IO;
     using System.Web; 
     using System.Web.UI; 
     using System.Drawing ;
     using System.Web.UI.WebControls; 
     using System.Web.UI.HtmlControls; 
     using System.Configuration;
     public class UploadFile
     {
      
      public bool SavePic(HttpPostedFile PostedFile,string PicPath,string PhysicalPath,out string Bgfile,int width,int height)
      {
       Bgfile="";
       
       System.Drawing.Image imgOri;
       string strFileext,strFile,strFilename,strBgfile;
       string strTmpfile;
         
       strFilename=PostedFile.FileName;
       strFileext=(Path.GetExtension(strFilename)).ToString().ToLower();
       strFile=Path.GetFileName(strFilename);
       DateTime Today=DateTime.Now;
       strTmpfile=Today.Year.ToString()+Today.Month.ToString()+Today.Day.ToString()+Today.Hour.ToString()+Today.Minute.ToString()+Today.Second.ToString()+Today.Millisecond.ToString();
       strBgfile="Bg"+strTmpfile;//得到文件名
         
       try
       {
        imgOri= System.Drawing.Image.FromStream(PostedFile.InputStream);
        //imgNew=imgOri.GetThumbnailImage(intFethumbwidth,intFethumbwidth * imgOri.Height/imgOri.Width,null,new System.IntPtr(0));
        //imgNew=imgOri.GetThumbnailImage(width,height,null,new System.IntPtr(0));
        
        //
        //新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(width,height);
        //新建一个画板
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
        
        //设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //清空一下画布
        g.Clear(Color.Blue);
        Bgfile=PicPath+strBgfile; //相对路径+文件名(数据库中的文件名)
        //在指定位置画图
        g.DrawImage(imgOri, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), new System.Drawing.Rectangle(0, 0, imgOri.Width,imgOri.Height),System.Drawing.GraphicsUnit.Pixel);
        
        strBgfile=PhysicalPath+strBgfile;//物理路径+文件名
       
        if (strFileext==".gif")
        {
         Bgfile+=".gif";
         
         strBgfile+=".gif";
         
         //imgNew.Save(strBgfile,System.Drawing.Imaging.ImageFormat.Gif);
         bitmap.Save(strBgfile, System.Drawing.Imaging.ImageFormat.Gif);
        }
        if (strFileext==".jpg")
        {
         Bgfile+=".jpg";
         
         strBgfile+=".jpg";
         
         //imgNew.Save(strBgfile,System.Drawing.Imaging.ImageFormat.Jpeg);
         bitmap.Save(strBgfile, System.Drawing.Imaging.ImageFormat.Jpeg);
         
        }
        g.Dispose();
       }
       catch (Exception E)
       {
        //return E.Message+" "+strBgfile;
        throw E;
       }
      
       return true;
      }//SavePic
      //生成两个图,一个原图,一个缩略图
      public bool SaveTowPic(HttpPostedFile PostedFile,string PicPath,string PhysicalPath,out string Bgfile,out string Smfile,int width,int height)
      {
       Bgfile="";
       Smfile="";
       System.Drawing.Image imgOri;
       string strFileext,strFile,strFilename,strBgfile,strSmfile;
       string strTmpfile;
         
       strFilename=PostedFile.FileName;
       strFileext=(Path.GetExtension(strFilename)).ToString().ToLower();
       strFile=Path.GetFileName(strFilename);
       DateTime Today=DateTime.Now;
       strTmpfile=Today.Year.ToString()+Today.Month.ToString()+Today.Day.ToString()+Today.Hour.ToString()+Today.Minute.ToString()+Today.Second.ToString()+Today.Millisecond.ToString();
       strBgfile="Bg"+strTmpfile;//得到文件名
       strSmfile="Sm"+strTmpfile;//小图文件名  
       try
       {
        imgOri= System.Drawing.Image.FromStream(PostedFile.InputStream);
        //imgNew=imgOri.GetThumbnailImage(intFethumbwidth,intFethumbwidth * imgOri.Height/imgOri.Width,null,new System.IntPtr(0));
        //imgNew=imgOri.GetThumbnailImage(width,height,null,new System.IntPtr(0));
        
        //
        //新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(width ,height);
        //新建一个画板
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
        //设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //清空一下画布
        g.Clear(Color.Blue);
        Bgfile=PicPath+strBgfile; //相对路径+文件名(数据库中的文件名)
        Smfile=PicPath+strSmfile;
        //在指定位置画图
        g.DrawImage(imgOri, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), new System.Drawing.Rectangle(0, 0, imgOri.Width,imgOri.Height),System.Drawing.GraphicsUnit.Pixel);
        
        strBgfile=PhysicalPath+strBgfile;//物理路径+文件名
        strSmfile=PhysicalPath+strSmfile;
        if (strFileext==".gif")
        {
         Bgfile+=".gif";
         Smfile+=".gif";
         strBgfile+=".gif";
         strSmfile+=".gif";
         imgOri.Save(strBgfile,System.Drawing.Imaging.ImageFormat.Gif);
         bitmap.Save(strSmfile, System.Drawing.Imaging.ImageFormat.Gif);
        }
        if (strFileext==".jpg")
        {
         Bgfile+=".jpg";
         strBgfile+=".jpg";
         Smfile+=".jpg";
         strSmfile+=".jpg";
         imgOri.Save(strBgfile,System.Drawing.Imaging.ImageFormat.Jpeg);//原图
         bitmap.Save(strSmfile, System.Drawing.Imaging.ImageFormat.Jpeg);//缩图
         
        }
        g.Dispose();
       }
       catch (Exception E)
       {
        //return E.Message+" "+strBgfile;
        throw E;
       }
      
       return true;
      }//SaveTowPic 
      public void DelPic(string strPic)
      {
       // /game/UpPic/Bgha200362410231493.gif
       HttpContext context=HttpContext.Current;
       string[] t1;
       string p1;
       t1=strPic.Split(new Char[] {‘/‘});
       p1=t1[t1.Length-2]+"\\"+t1[t1.Length-1];
       p1=context.Request.PhysicalApplicationPath+p1;
       if(File.Exists(p1))
        File.Delete(p1);
        
      }
      public void DelPic2(string strPic)
      {
       // /game/UpPic/Bgha200362410231493.gif
       HttpContext context=HttpContext.Current;
       strPic=context.Request.PhysicalApplicationPath+"\\"+strPic;
       if(File.Exists(strPic))
        File.Delete(strPic);
        
      }
      
     }//class
    }
     
    (在对代码进行拷贝时需要注意有些特殊符号过滤了,需要还原)
    具体调用:例如删除事件
    string t="vote\\"+((Label)e.Item.FindControl("lbl_name1")).Text;
       string t2="vote\\"+((Label)e.Item.FindControl("lbl_name2")).Text;
       UploadFile test=new UploadFile();
       test.DelPic2(t);
       test.DelPic2(t2);
       databind_vote_pic();
       ((Label)e.Item.FindControl("lbl_del")).Text="已删除,请刷新";
       this.lbl_mes.Text="已删除,请刷新";
      

  3.   

    指定了图像的质量图像效果也不是很好,以下是步骤
    1.获取原始图像
    2.创建一块画布bitmap,画布大小为缩略图大小,这里比较麻烦的是按比例缩放,计算比例时要准确,这是最重要的一步
    3.创建画布的Graphics 对象g
    4.g.DrawImage(原始图像,new Rectangle(0,0,缩略图的宽度,缩略图的高度)),这样绘出来的图像和原图质量基本一样
    5.保存画布