简体   繁体   English

为上传的图像创建缩略图

[英]Create Thumbnails for Uploaded Images

I have an asp.net mvc app where I am able to upload multiple images at once and that works fine, but I have an issue with creating thumbnail images out of the originals. 我有一个asp.net mvc应用程序,可以一次上传多个图像,而且效果很好,但是在从原始图像创建缩略图时遇到了问题。

Here's the code: 这是代码:

[HttpPost]
        public ActionResult SaveUploadedFile()
        {
            bool isSavedSuccessfully = true;
            string fName = "";

            try
            {
                foreach (string fileName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[fileName];

                    fName = file.FileName;

                    if (file != null && file.ContentLength > 0)
                    {
                        var originalDirectory = new DirectoryInfo(string.Format("{0}Images", Server.MapPath(@"\")));

                        string pathString = Path.Combine(originalDirectory.ToString(), "Gallery");
                        string pathString2 = Path.Combine(originalDirectory.ToString(), "Gallery\\Thumbs");

                        //var fileName1 = Path.GetFileName(file.FileName);

                        bool exists = Directory.Exists(pathString);
                        bool exists2 = Directory.Exists(pathString2);

                        if (!exists)
                            Directory.CreateDirectory(pathString);

                        if (!exists2)
                            Directory.CreateDirectory(pathString2);

                        var path = string.Format("{0}\\{1}", pathString, file.FileName);

                        file.SaveAs(path);

                        //WebImage img = new WebImage(file.InputStream);
                        WebImage img = new WebImage(fileName);
                        //if (img.Width > 1000)
                        img.Resize(100, 100);
                        img.Save(pathString2);

                    }

                }

            }
            catch (Exception ex)
            {
                isSavedSuccessfully = false;
            }

            if (isSavedSuccessfully)
            {
                return Json(new { Message = fName });
            }
            else
            {
                return Json(new { Message = "Error in saving file" });
            }
        }

So basically file.SaveAs(path); 所以基本上是file.SaveAs(path); is the last line that works and I actually save the images, but after that line I try to create and save thumbs but it does not work (I do get the Thumbs folder created before though). 是有效的最后一行,我实际上保存了图像,但是在那一行之后,我尝试创建并保存缩略图,但它不起作用(不过我确实获得了之前创建的Thumbs文件夹)。

I also get the Error in saving file response but that is because I am trying to create and save the thumbs, if I remove that then I don't get it, and I do not know how to return the actual error to actually see what the error is. Error in saving file响应时也遇到Error in saving file但这是因为我试图创建并保存拇指,如果我删除了拇指,那么我将不明白,并且我不知道如何返回实际错误以实际查看结果错误是。

The answer is this: 答案是这样的:

var path2 = string.Format("{0}\\{1}", pathString2, file.FileName)

WebImage img = new WebImage(file.InputStream);
img.Resize(250, 250);
img.Save(path2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM