C# 上传图像、重命名图像、制作缩略图并替换原始图像。(优化)

C# 上传图像、重命名图像、制作缩略图并替换原始图像。(优化),c#,asp.net-mvc,file-io,file-upload,C#,Asp.net Mvc,File Io,File Upload,我已经创建了我在问题中描述的这些函数。然而,我认为我这样做的方式并不是最好的方式 [HttpPost] public ActionResult Create(FormCollection collection, string schooljaarparam, FlatONASAanbieder foa) { if (ModelState.IsValid) { // var r = new List<ViewData

我已经创建了我在问题中描述的这些函数。然而,我认为我这样做的方式并不是最好的方式

        [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));
    }
所以我的做法是:

  • 我从浏览器中获取文件
  • 我检查它是否是图像
  • 我检查文件是否存在,如果存在,则相应地更改文件名
  • 我将文件保存在磁盘上(IO,慢速)
  • 我以图像的形式打开文件
  • 我用VerkleinMaxHoogte方法计算宽度和高度
  • 我创建缩略图并用tmp扩展名保存它
  • 我删除了原始文件
  • 我将缩略图重命名为原始文件名(这是我想要的)

  • 如何更快地完成此操作?

    您可以始终使用和方法将#4和#5组合起来。这也将消除#8和#9。

    好的观点。我在找这样的东西,但没找到:)
        public static bool isImage(string s) {
            if (s.EndsWith(".jpg", true, null) || s.EndsWith(".jpeg", true, null) || s.EndsWith(".gif", true, null) || s.EndsWith(".png", true, null)) {
                return true;
            }
            return false;
        }