Home > Uncategorized > Generating thumbnails in ASP.NET

Generating thumbnails in ASP.NET


I found just developed a nice new function for uploading images from a website, and generating thumbnails on the fly

 public string storeFile(HttpPostedFile postedFile)
   string strFilename="";
   string strPath = "c:\wherever\";
   if( postedFile.FileName != "" )
 HttpPostedFile hpfFile = postedFile;
    int nFileLen = hpfFile.ContentLength;
 byte[] bData = new byte[nFileLen];
 hpfFile.InputStream.Read(bData, 0, nFileLen);
    strFilename = Path.GetFileName(hpfFile.FileName);
 FileStream fsFullImage = new FileStream(strPath + strFilename , FileMode.Create);
 fsFullImage.Write(bData, 0, bData.Length);
    System.Drawing.Image  iPhoto;
 System.Drawing.Image  iThumbnail;
    iPhoto = System.Drawing.Image.FromFile(strPath + strFilename);
   iThumbnail = iPhoto.GetThumbnailImage(100, 100, null, IntPtr.Zero);
 FileStream fsThumbnailImage = new FileStream(strPath + "tn" + strFilename , FileMode.Create);
 iThumbnail.Save(fsThumbnailImage, ImageFormat.Gif);
   return strFilename;

This is then called in a asp.net page with

string strFilename = storeFile(Photo.PostedFile);

Where Photo is a name of a file input box such as:

<input type=file name="Photo" id="Photo" runat="server">

You also have to add the following – enctype="multipart/form-data" to your form, and ensure you have write permissions on the destination folder.


Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: