Generating thumbnails in ASP.NET
Hi,
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);
fsFullImage.Close();
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);
fsThumbnailImage.Close();
}
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.