Simon Hibbard's Web Apps Blog

Rescale and save Image on the server using ASP.Net GetThumbnailImage


A quick function I use to save a rescaled version of an image during upload.

Handle the upload click and do the rescaling:

protected void UploadFileClick(object sender, EventArgs e)
{
    // get the upload control
    FileUpload FileUpload1 = (FileUpload)ColourFV.FindControl("FileUpload1");
    // get the filename
    String strFileName1 = FileUpload1.PostedFile.FileName;

    if (strFileName1 != "")
    {
        //Grab the file name from its fully qualified path at client 
        TextBox FilenameProfileTbx1 = (TextBox)ColourFV.FindControl("FilenameProfileTbx1");
        //only the attached file name not its path
        String fileName2 = System.IO.Path.GetFileName(strFileName1);

        //Save uploaded file to server 
        try
        {
            FileUpload1.PostedFile.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["your setting"]) + fileName1);
        }
        catch (Exception ex)
        {
            // do any error handling u like here
        }

        // Now this function is called to rescale and save 
        RescaleAndSaveImge((Server.MapPath(ConfigurationManager.AppSettings["your setting"]) + fileName1),
            (Server.MapPath(ConfigurationManager.AppSettings["your setting"]) + "Thumbnails/" + fileName1), 155, 81);

    }

Here is the code for the function to rescale:

    public bool ThumbnailCallback()
    {
        // do any error handling here
        return false;
    }
    
    // TargetImgFile = filepath of image saved to the server which you want to rescale
    // DestImgFile = filepath of the new image you want to save to
    // NewWidth = new width in px 
    // NewHeight = new height in px
    public void RescaleAndSaveImge(String TargetImgFile, String DestImgFile, Int32 NewWidth, Int32 NewHeight)
    {
        System.Drawing.Image.GetThumbnailImageAbort myCallback = 
            new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); 

        using(System.Drawing.Image TargetImage =
            System.Drawing.Image.FromFile(TargetImgFile))
        { 
            using (System.Drawing.Image Thumbnail = 
                TargetImage.GetThumbnailImage(NewWidth, NewHeight, myCallback, IntPtr.Zero)) 
            {
                Thumbnail.Save(DestImgFile); 
                Thumbnail.Dispose(); 
            } 
            TargetImage.Dispose(); 
        } 

    }
Advertisements

One response

  1. This demo does not work with Google Chrome. Otherwise very nice.

    July 26, 2010 at 11:24 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s