Upload image, rename it, make thumbnail and replace the original. (optimization)
I have created these functions which I described in the question. However I think the way I did it is not the optimal way of doing it.
[HttpPost]
public ActionResult Create(FormCollection collection, string schooljaarparam, FlatONASAanbieder foa) {
if (ModelState.IsValid) {
// var r = new List<ViewDataUploadFilesResult>();
foreach (string file in Request.Files) {
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
//extensie nakijken. jpg, png, jpeg, of GIF.
if (MvcApplication.isImage(hpf.FileName)) {
//Image img = new Image();
string savedFileName = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory + "uploads\\ONAS\\",
Path.GetFileName(hpf.FileName));
FileInfo fi = new FileInfo(savedFileName);
int i = 1;
while (fi.Exists) {
fi = new FileInfo(savedFileName.Substring(0, savedFileName.Length - Path.GetFileName(savedFileName).Length) + Path.GetFileNameWithoutExtension(savedFileName) + " (" + i++ + ") " + Path.GetExtension(savedFileName));
}
savedFileName = fi.DirectoryName + "\\" + fi.Name;
hpf.SaveAs(savedFileName);
using (Image Img = Image.FromFile(savedFileName)) {
//Size ThumbNailSize = NewImageSize(Img.Height, Img.Width, 79);
Size NewSize = VerkleinMaxHoogte(Img.Size, 79);
using (Image ImgThnail = new Bitmap(Img, NewSize.Width, NewSize.Height)) {
//string ss = savedFileName.Substring(0, savedFileName.Length - Path.GetFileName(savedFileName).Length) + Path.GetFileNameWithoutExtension(savedFileName) + "-thumb" + Path.GetExtension(savedFileName);
ImgThnail.Save(savedFileName + ".tmp", Img.RawFormat);
ImgThnail.Dispose();
}
Img.Dispose();
}
System.IO.File.Delete(savedFileName);
FileInfo f = new FileInfo(savedFileName + ".tmp");
f.MoveTo(savedFileName);
} else {
ModelState.AddModelError("ONAS_Logo", "Het geuploadde bestand is geen afbeelding. ");
}
//r.Add(new ViewDataUploadFilesResult() {
// Name = savedFileName,
// Length = hpf.ContentLength
//});
}
}
// return View("UploadedFiles", r);
return View();
}
[NonAction]
public Size VerkleinMaxHoogte(Size orig, double height) {
double tempval = height / orig.Height;
return new Size(Convert.ToInt32(tempval * orig.Width), Convert.ToInt32(height));
}
in global.asax
public static bool isImage(string s) {
if (s.EndsWith(".jpg", true, null) || s.EndsWith(".jpeg", true, null) || s.EndsWith(".gif", true, null) || s开发者_StackOverflow中文版.EndsWith(".png", true, null)) {
return true;
}
return false;
}
so the way I do it:
- I get the file from the browser
- I check if it is an Image
- I check if the file exists, and if so, change the filename accordingly
- I save the file on disk (IO, slow)
- I open the file as an image
- I calculate the width and height with the VerkleinMaxHoogte method
- I create the thumbnail and save it with a tmp extension
- I delete the original file
- I rename the thumbnail to the original file name (this is what I want)
How do I do it faster?
You can always use HttpPostedFile.InputStream and Image.FromStream method to combine #4 & #5. This will also eliminate #8 & #9.
精彩评论