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

Published 08 March 2007 05:58 PM by haissam

Comments

# Rinaldi said on 28 March, 2007 06:51 AM

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

Thank you very much Hassam.

# haissam said on 28 March, 2007 08:30 AM

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

# Rinaldi said on 28 March, 2007 04:38 PM

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.  

# haissam said on 28 March, 2007 05:13 PM

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

# aashish said on 14 July, 2009 08:15 AM

Hi i am new to asp.net and i am using ur code with fileupload control and i m having a problem my code is (with error i m getting)

---------------------------------

               Code

Protected Sub cmdUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdUpload.Click

       If FileUpload1.HasFile = True Then

           Dim ext As String

           Dim st As String = StrReverse(FileUpload1.FileName)

           ext = StrReverse(st.Substring(0, 3))

           'FileUpload1.PostedFile.SaveAs("D:\WebApplications\Employee Portals\EmpImages\" + txtCardNo.Text & "." & ext)

           Dim displayedImg As String = FileUpload1.PostedFile.FileName

           ' '' ''Dim a As String = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)

           Dim displayedImgThumb As String = Server.MapPath("~\EmpImages\")

           ' '' ''FileUpload1.PostedFile.SaveAs(displayedImgThumb)

           '// Get the path of the Thumb folder

           ' Get the original image file name

           Dim imgFileName As String = txtCardNo.Text & "." & ext 'System.IO.Path.GetFileName(displayedImg)

           ' Load original image

           Dim myimg As System.Drawing.Image = System.Drawing.Image.FromFile(displayedImg)

           ' Get the thumbnail 100 X 100 px

           myimg = myimg.GetThumbnailImage(110, 125, Nothing, IntPtr.Zero)

           ' Save the new thumbnail image

           myimg.Save(displayedImgThumb + imgFileName, myimg.RawFormat)

           Image5.ImageUrl = "~/EmpImages/" + txtCardNo.Text & "." & ext

           FMsg.Message = "Image Uploaded"

           FMsg.CssClass = "errormsg"

           FMsg.Display()

       End If

   End Sub

               Error

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: C:\Documents and Settings\ashish\Desktop\testtt.JPG

Source Error:

Line 635:

Line 636:            ' Load original image

Line 637:            Dim myimg As System.Drawing.Image = System.Drawing.Image.FromFile(displayedImg)

Line 638:

Line 639:

Source File: D:\WebApplications\EmployeePortal\EmployeeDetail.aspx.vb    Line: 637

Stack Trace:

[FileNotFoundException: C:\Documents and Settings\ashish\Desktop\testtt.JPG]

  System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) +379411

  System.Drawing.Image.FromFile(String filename) +6

  EmployeeDetail.cmdUpload_Click(Object sender, EventArgs e) in D:\WebApplications\EmployeePortal\EmployeeDetail.aspx.vb:637

  System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +111

  System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +79

  System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10

  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13

  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175

  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

----------------------------------

Plz help me as early as possible

My code is working fine on local machine but when i am uploading my form then i am getting the it gives the following error

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP
  • Social Bookmarking
    Online Shopping
    asp.net hosting
    UK online local dating