Create Image Thumbnail on the fly

While working on a Photo gallery project, i realized the power of .NET to enable the developer to create thumbnail images in few lines of code. This gave me the advantage to load my webforms without waiting the whole image to be download to the user. Working with big size images is considered a real pain specially when using a slow internet connection. So let's jump directly to the code.

    // Get the path of the original Image
     string displayedImg = Server.MapPath("~") + "/Testing.jpg";

    // Get the path of the Thumb folder
    string displayedImgThumb = Server.MapPath("~") + "/Thumb/";
    
    // Get the original image file name
    string imgFileName = System.IO.Path.GetFileName(displayedImg);
 
    // Load original image
    System.Drawing.Image myimg = System.Drawing.Image.FromFile(displayedImg);

    // Get the thumbnail 100 X 100 px
    myimg = myimg.GetThumbnailImage(100, 100, null, IntPtr.Zero);
    
   // Save the new thumbnail image
   myimg.Save(displayedImgThumb + imgFileName, myimg.RawFormat);

Best Regards,

HC

Comments

# re: Create Image Thumbnail on the fly

Wednesday, March 28, 2007 6:51 AM by Rinaldi

That's great. I was wondering if you could let know where I can download the code.

Thank you very much Hassam.

# re: Create Image Thumbnail on the fly

Wednesday, March 28, 2007 8:30 AM by haissam

Hello Rinaldi,

Thank you for your comment, in the post above as you may realized i wrote the neccessary code to create a thumbnail. If you need the whole project, i will upload it.

Best Regards,

HC

# re: Create Image Thumbnail on the fly

Wednesday, March 28, 2007 4:38 PM by Rinaldi

Hey Haissam now that I realized where "myimg" is instantiated, because I'm used with Vb.

Thank you very much. It's helps a lot. And I will try to convert the Reach text content to Vb, when it's done I let you know.  

# re: Create Image Thumbnail on the fly

Wednesday, March 28, 2007 5:13 PM by haissam

Cool, i was intending to convert the rich text editor to vb.net but i'm so busy these days.

Once you finish let me know to provide it share it with the community

Best Regards,

HC

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.