Re: image thumbnail

  •  08-11-2009, 3:41 AM

    Re: image thumbnail

    Hi,
     
    You can try this code :
     
    1. private void GenerateThumbnail(string filepath, string thumbnailPath)   
    2. {   
    3.     byte[] data;   
    4.     using(FileStream fs=new FileStream(filepath,FileMode.Open,FileAccess.Read,FileShare.Read))   
    5.     {   
    6.         data=new byte[fs.Length];   
    7.         fs.Read(data,0,data.Length);   
    8.     }   
    9.     using(System.Drawing.Image img = System.Drawing.Image.FromStream(new MemoryStream(data)))   
    10.     {   
    11.         double size=128d;   
    12.         double scale = Math.Min(  size/img.Width, size/img.Height );   
    13.         int w = (int)(img.Width * scale);   
    14.         int h = (int)(img.Height * scale);   
    15.   
    16.         using(System.Drawing.Image thumb=new System.Drawing.Bitmap(w,h,System.Drawing.Imaging.PixelFormat.Format24bppRgb))   
    17.         {   
    18.             using(System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(thumb))   
    19.             {   
    20.                 g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;   
    21.                 g.DrawImage(img   
    22.                     , new System.Drawing.Rectangle(-1, -1, w+2, h+2)   
    23.                     , new System.Drawing.Rectangle(0, 0, img.Width, img.Height)   
    24.                     , System.Drawing.GraphicsUnit.Pixel);   
    25.                    
    26.                 thumb.Save(thumbnailPath);     
    27.             }                                          
    28.         }   
    29.     }   
    30. }  

    Regards,
    Terry
     
View Complete Thread