image thumbnail

Last post 08-11-2009, 3:41 AM by cutechat. 1 replies.
Sort Posts: Previous Next
  •  08-11-2009, 3:23 AM 54628

    image thumbnail

    First of all, this is a excellent product. My only concern is how to save a thumbnail image when uploading image file. I tried creating a simple function and it works fine locally, when deployed on web server, the browser just stops and only the image file uploaded is uploaded to the folder, not the thumbnail.
     
    void SaveImageThumbNail(string ImageFileName, Int32 thumbnailWidth, string loc)
        {


            System.Drawing.Image objImage;
            System.Drawing.Image objThumbnail;
            string strServerPath = "";
            string strFilename = "";
            Int32 shtWidth;
            Int32 shtHeight;


            strServerPath = loc;

            strFilename = ImageFileName;

            try
            {
                if (ImageFileName.Contains("jpg") || ImageFileName.Contains("gif") || ImageFileName.Contains("png") || ImageFileName.Contains("bmp"))
                {
                    objImage = System.Drawing.Image.FromFile(strServerPath + strFilename);
                }
                else
                {
                    objImage = System.Drawing.Image.FromFile(strServerPath + "noimage.gif");
                }
            }

            catch
            {
                objImage = System.Drawing.Image.FromFile(strServerPath + "noimage.gif");

            }

            try
            {
                shtWidth = thumbnailWidth;
                shtHeight = objImage.Height / (objImage.Width / shtWidth);

                objThumbnail = objImage.GetThumbnailImage(shtWidth, shtHeight, null, System.IntPtr.Zero);

                //Response.ContentType = "image/jpeg";
                objThumbnail.Save(strServerPath + "thb_" + ImageFileName);

                objImage.Dispose();
                objThumbnail.Dispose();

            }
            catch
            {

            }


        }
     
     
    and I tried attaching it here
     
    void Uploader_FileUploaded(object sender, UploaderEventArgs args)
        {
            Uploader uploader = (Uploader)sender;
            InsertMsg("File uploaded! " + args.FileName + ", " + args.FileSize + " bytes.");

            args.CopyTo(Request.Cookies["fileLoc"].Value.ToString() + args.FileName);
            SaveImageThumbNail(args.FileName, 100, Request.Cookies["fileLoc"].Value.ToString());
           
        }
     
     
    Just want to ask if you have a much simplier solution on how to save thumbnail file for images. Thank You.
     
     
     
  •  08-11-2009, 3:41 AM 54629 in reply to 54628

    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 as RSS news feed in XML