如何调整大小并保存使用c#中的文件上传控件上传的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17079090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:31:21  来源:igfitidea点击:

How to resize and save an image which uploaded using file upload control in c#

c#asp.net-mvc-4file-uploadrazorimage-resizing

提问by sanzy

i have developed a web application using asp.net mvc4 and razor. in my application there's a file upload control to upload an image and save in a temporary location.

我使用asp.net mvc4 和razor 开发了一个Web 应用程序。在我的应用程序中,有一个文件上传控件可以上传图像并保存在临时位置。

before save image should re-sized to a specific size and then save in the temporary location given.

在保存图像之前,应将图像重新调整为特定大小,然后保存在给定的临时位置。

here is the code i have used in controller class.

这是我在控制器类中使用的代码。

public class FileUploadController : Controller
{
    //
    // GET: /FileUpload/

    public ActionResult Index()
    {
        return View();
    }
    public ActionResult FileUpload()
    {
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult FileUpload(HttpPostedFileBase uploadFile)
    {
        if (uploadFile.ContentLength > 0)
        {
            string relativePath = "~/img/" + Path.GetFileName(uploadFile.FileName);
            string physicalPath = Server.MapPath(relativePath);


            FileUploadModel.ResizeAndSave(relativePath, uploadFile.FileName, uploadFile.InputStream, uploadFile.ContentLength, true);

            return View((object)relativePath);
        }
        return View();
    }
}

and here is the code used in model class

这是模型类中使用的代码

public class FileUploadModel
{
    [Required]
    public HttpPostedFileWrapper ImageUploaded { get; set; }

    public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare)
    {
        int newWidth;
        int newHeight;
        Image image = Image.FromStream(imageBuffer);
        int oldWidth = image.Width;
        int oldHeight = image.Height;
        Bitmap newImage;
        if (makeItSquare)
        {
            int smallerSide = oldWidth >= oldHeight ? oldHeight : oldWidth;
            double coeficient = maxSideSize / (double)smallerSide;
            newWidth = Convert.ToInt32(coeficient * oldWidth);
            newHeight = Convert.ToInt32(coeficient * oldHeight);
            Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
            int cropX = (newWidth - maxSideSize) / 2;
            int cropY = (newHeight - maxSideSize) / 2;
            newImage = new Bitmap(maxSideSize, maxSideSize);
            Graphics tempGraphic = Graphics.FromImage(newImage);
            tempGraphic.SmoothingMode = SmoothingMode.AntiAlias;
            tempGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            tempGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
            tempGraphic.DrawImage(tempImage, new Rectangle(0, 0, maxSideSize, maxSideSize), cropX, cropY, maxSideSize, maxSideSize, GraphicsUnit.Pixel);
        }
        else
        {
            int maxSide = oldWidth >= oldHeight ? oldWidth : oldHeight;

            if (maxSide > maxSideSize)
            {
                double coeficient = maxSideSize / (double)maxSide;
                newWidth = Convert.ToInt32(coeficient * oldWidth);
                newHeight = Convert.ToInt32(coeficient * oldHeight);
            }
            else
            {
                newWidth = oldWidth;
                newHeight = oldHeight;
            }
            newImage = new Bitmap(image, newWidth, newHeight);
        }
        newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg);
        image.Dispose();
        newImage.Dispose();
    }
}

but when i run the application it occurs an ArgumentException.

但是当我运行应用程序时,它发生了ArgumentException

it says "Parameter is not valid"in following code line

它在以下代码行中说“参数无效”

Bitmap tempImage = new Bitmap(image, newWidth, newHeight);

how do i pass valid and appropriate parameters here

我如何在这里传递有效和适当的参数

public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare)

采纳答案by maxs87

Its very difficult to understand what is the problem with your code. But may be you want to use alternative way. You need to add the reference to System.Web.Helpers namespace and try the following code.

很难理解您的代码有什么问题。但可能您想使用替代方式。您需要添加对 System.Web.Helpers 命名空间的引用并尝试以下代码。

[HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        WebImage img = new WebImage(file.InputStream);
        if (img.Width > 1000)
            img.Resize(1000, 1000);
        img.Save("path");
        return View();
    }

Also this class supports the crop, flip, watermark operation etc.

该类还支持裁剪、翻转、水印等操作。