C# 如何在c中使用文件上载控件调整和保存上载的图像#

C# 如何在c中使用文件上载控件调整和保存上载的图像#,c#,asp.net-mvc-4,file-upload,razor,image-resizing,C#,Asp.net Mvc 4,File Upload,Razor,Image Resizing,我使用asp.NETMVC4和razor开发了一个web应用程序。在我的应用程序中,有一个文件上载控件,用于上载图像并保存在临时位置 在保存之前,图像应重新调整到特定大小,然后保存到给定的临时位置 这是我在控制器类中使用的代码 public class FileUploadController : Controller { // // GET: /FileUpload/ public ActionResult Index() { return V

我使用asp.NETMVC4和razor开发了一个web应用程序。在我的应用程序中,有一个文件上载控件,用于上载图像并保存在临时位置

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

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

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();
    }
}
下面是模型类中使用的代码

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();
    }
}
但是,当我运行应用程序时,它会出现一个ArgumentException

在下面的代码行中显示“参数无效”

Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
如何在此处传递有效和适当的参数

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

很难理解代码的问题是什么。但也许你想用另一种方式。您需要添加对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();
    }

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

简直太棒了!谢谢你好谢谢你上传这篇文章。但是如果图像大小小于1MB,你的代码就可以正常工作。如果我上传的图像大小小于3MB或2MB,就会显示错误消息“内存不足”。你能帮我吗?谢谢!正是我要找的嗨。。在本地机器上,它工作正常,但每当我部署它时,它就会抛出网页,这是一个异常。@user3217843您的问题可以通过增加运行时间来解决。由于系统上载2MB文件所需时间稍长,因此要解决此问题,请在“写入”下转到您的Web.Config。这样做将增加系统的时间,并解决您的内存问题