如何调整 HttpPostedFileWrapper 的大小

本文关键字:HttpPostedFileWrapper 调整 何调整 | 更新日期: 2023-09-27 18:37:02

我需要按比例调整此图像的大小 411px。这是怎么做到的?

[HttpPost]
        public WrappedJsonResult UploadImage(HttpPostedFileWrapper imageFile, int id)
        {
            if (imageFile == null || imageFile.ContentLength == 0)
            {
                return new WrappedJsonResult
                {
                    Data = new
                    {
                        IsValid = false,
                        Message = "No file was uploaded.",
                        ImagePath = string.Empty
                    }
                };
            }
            var fileName = String.Format("{0}_{1}.jpg", id, Guid.NewGuid().ToString());
            var imagePath = Path.Combine(Server.MapPath(Url.Content("~/Content/UploadPhoto")), fileName);
            imageFile.SaveAs(imagePath);
}

如何调整 HttpPostedFileWrapper 的大小

public ActionResult UploadImage(HttpPostedFileBase imageFile, int id)
{
    if (imageFile == null || imageFile.ContentLength == 0)
    {
        return new WrappedJsonResult
        {
            Data = new
            {
                IsValid = false,
                Message = "No file was uploaded.",
                ImagePath = string.Empty
            }
        };
    }
    var fileName = String.Format("{0}_{1}.jpg", id, Guid.NewGuid().ToString());
    var imagePath = Path.Combine(Server.MapPath(Url.Content("~/Content/UploadPhoto")), fileName);
    using (var input = new Bitmap(imageFile.InputStream))
    {
        int width;
        int height;
        if (input.Width > input.Height)
        {
            width = 411;
            height = 411 * input.Height / input.Width;
        }
        else
        {
            height = 411;
            width = 411 * input.Width / input.Height;
        }
        using (var thumb = new Bitmap(width, height))
        using (var graphic = Graphics.FromImage(thumb))
        {
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphic.SmoothingMode = SmoothingMode.AntiAlias;
            graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphic.DrawImage(input, 0, 0, width, height);
            using (var output = System.IO.File.Create(imagePath))
            {
                thumb.Save(output, ImageFormat.Jpeg);
            }
        }
    }
    ...
}

顺便说一下,您会注意到我在操作签名中将HttpPostedFileWrapper替换为HttpPostedFileBase,因为这是正确的使用类型。

相关文章:
  • 没有找到相关文章