Shaofh

Shaofh

C#生成缩略图

//注意引入 using System.Drawing.Imaging;
public bool ThumbnailCallback()
  {
   return false;
  } 
////oldfile原图地址,  newfile新图地址
  private void ShowThumbnail(string oldfile,string newfile)
  {  
   System.Drawing.Image image =System.Drawing.Image.FromFile(oldfile);
       //获取原图高度和宽度
         int oldh = image.Height;
         int oldw = image.Width;
         int neww,newh;
        neww=100;newh=100;   //直接设定新图的高宽,,等比缩放代码略,
     try
     {
           System.Drawing.Image.GetThumbnailImageAbort callb=new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
          System.Drawing.Image bt=new System.Drawing.Bitmap(neww,newh);
          System.Drawing.Graphics gr=System.Drawing.Graphics.FromImage(bt);
           gr.Clear(Color.White);
           gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
           gr.DrawImage(image, new Rectangle(0,0,neww,newh), 0, 0, oldw,oldh, GraphicsUnit.Pixel);
          switch(oldfile.Substring(oldfile.Length-3).ToUpper())
          {
                case "JPG":
                bt.Save(newfile,ImageFormat.Jpeg);
                break;
                case "GIF":
                 bt.Save(newfile,ImageFormat.Gif);
                 break;
                 case "PNG":
                 bt.Save(newfile,ImageFormat.Png);
                 break;
                 default:
                 bt.Save(newfile,ImageFormat.Jpeg);
                  break;
            } 
            gr.Dispose();
            bt.Dispose();
            image.Dispose();
     }
   catch{}
 }

posted on 2006-08-11 22:26  sh37  阅读(590)  评论(0编辑  收藏  举报

导航