using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
public partial class admin_AddPhoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string reportPath = Server.MapPath("../picnic");
if (!Directory.Exists(reportPath))
{
Directory.CreateDirectory(Server.MapPath("../picnic"));
}
}
protected void PhotoForm_ItemInserting(object sender, FormViewInsertEventArgs e)
{
FormView uploadForm = sender as FormView;
FileUpload uploadedFile = uploadForm.FindControl("uploadedFile") as FileUpload;
if (uploadedFile != null)
{
string fileName = uploadedFile.PostedFile.FileName;
string pathFile = System.IO.Path.GetFileName(fileName);
try
{
uploadedFile.SaveAs(Server.MapPath("../picnic/") + pathFile);
}
catch (Exception exp)
{
//catch exception here
}
try
{
Bitmap uploadedimage = new Bitmap(uploadedFile.PostedFile.InputStream);
e.Values["ImageWidth"] = uploadedimage.Width.ToString();
e.Values["ImageHeight"] = uploadedimage.Height.ToString();
// Make output File Name
char[] splitter = { '.' };
string[] splitFile = pathFile.Split(splitter);
string OutputFilename = splitFile[0] + "s";
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image thumbImage = uploadedimage.GetThumbnailImage(74, 54, myCallback, IntPtr.Zero);
thumbImage.Save(Server.MapPath("../picnic/") + OutputFilename + ".jpg");
e.Values["Thumbnail"] = "./picnic/" + OutputFilename + ".jpg";
}
catch (Exception ex)
{
//catch exception here
}
e.Values["Pic"] = "./picnic/" + pathFile;
e.Values["Url"] = "./picnic/" + pathFile;
e.Values["dateEntered"] = DateTime.Now.ToString();
}
}
public bool ThumbnailCallback()
{
return false;
}
}
This uses a FileUpload and a FormView to insert. Then I use the GetThumnailImage() method provided in System.Drawing.Imaging. You can enter any Width and Height values and it will shrink/stretch accordingly.
uploadedimage.GetThumbnailImage(W, H, myCallback, IntPtr.Zero);
Hope this helps.