I just copied and pasted this from a working site writen in C# using basic asp.net controls. I have put a few comments in to help make things a bit clearer.
protected void btn_Upload_Click(object sender, EventArgs e)
{
if (this.FileUpload1.PostedFile.ContentLength <= 0)
{
return;
}
//Gets the next valid ID from the database
Ocean2.Web.Dal.Member.Comic_Images Image = new Ocean2.Web.Dal.Member.Comic_Images();
Image.Comicid = this.ComicId;
Image.Name = this.FileUpload1.PostedFile.FileName;
Image = Ocean2.Web.Dal.Member.Comic_Images.Insert(Image);
if (Image.Id <= 0)
{
return;
}
//Sets up the call back for the thumbnail code
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
//Just creates the file path to save the files
string Path = System.Configuration.ConfigurationSettings.AppSettings["ComicImages"];
string ComicImagepath = string.Format(Path + "ComicImage{1}_{0}", Image.Id, Image.Comicid);
//Takes the original image and puts it into a bitmap class (I usually only work with png images so this is safe for me)
System.Drawing.Bitmap ComicImage = new System.Drawing.Bitmap(this.FileUpload1.PostedFile.InputStream);
//creates two new file streams for the different sizes
System.IO.FileStream MediumStream = new System.IO.FileStream(ComicImagepath + "_2.jpg", System.IO.FileMode.CreateNew);
System.IO.FileStream ThumbStream = new System.IO.FileStream(ComicImagepath + "_3.jpg", System.IO.FileMode.CreateNew);
//creates the thumbnails at the two smaller sizes.
System.Drawing.Image MediumImage = ComicImage.GetThumbnailImage(230, 400, myCallback, IntPtr.Zero);
System.Drawing.Image ThumbImage = ComicImage.GetThumbnailImage(115, 200, myCallback, IntPtr.Zero);
//saves and closes the files and disposes of any objects
//these class's last time I checked do use GDI objects so always make sure to dispose of them properly.
MediumImage.Save(MediumStream, System.Drawing.Imaging.ImageFormat.Jpeg);
MediumStream.Close();
ThumbImage.Save(ThumbStream, System.Drawing.Imaging.ImageFormat.Jpeg);
ThumbStream.Close();
ThumbImage.Dispose();
MediumImage.Dispose();
this.Response.Redirect("example.aspx?id=" + ComicId);
}
public bool ThumbnailCallback()
{
return false;
}
Reference
Image.GetThumbnailImage Method [msdn.microsoft.com]