public bool SaveImage(HttpPostedFile image, string title)
{
bool result = false;
int MaxWidth = 500;
int MaxHeight = 500;
int ThumbMaxWidth = 150;
int ThumbMaxHeight = 150;
// Ensure a unique filename
string upFileName = new Guid().ToString() + image.FileName.Substring(image.FileName.LastIndexOf("."));
// Should put this into a config file.
string LargeFile = @"UserImages\Large\" + upFileName;
string ThumbFile = @"UserImages\Thumbs\" + upFileName;
try
{
System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(image.InputStream);
System.Drawing.Image LargeImage = ImageTool.ToMaxSize(UploadedImage, MaxWidth, MaxHeight);
System.Drawing.Image ThumbImage = ImageTool.ToMaxSize(UploadedImage, ThumbMaxWidth, ThumbMaxHeight);
LargeImage.Save(Request.PhysicalApplicationPath + LargeFile);
ThumbImage.Save(Request.PhysicalApplicationPath + ThumbFile);
}
catch (Exception ex)
{
return false;
}
GalleryDataContext db = new GalleryDataContext();
// Save the image paths and title to the database using LINQ
GalleryImage i = new GalleryImage();
i.FullSize = LargeFile.Replace('\\', '/');
i.Thumbnail = ThumbFile.Replace('\\', '/');
i.Title = title;
try
{
db.GalleryImages.InsertOnSubmit(i);
db.SubmitChanges();
}
catch (Exception ex)
{
return false;
}
return result;
}
<http://www.robbihun.com/Post/ASPNET-Uploading-and-Resizing-Images>