上传图片文件大小大于实际文件大小

Upload image file size is more than actual file size

我正在使用 fileapi 插件在 c# 中上传图像。我的实际文件大小为 60kb,但上传后文件大小在服务器上显示为 350kb。为什么会这样?这是我保存图像的代码:

    public JsonResult SaveImageFile(byte[] file)
    {
        var filesData = Request.Files[0];
        string fileName = System.DateTime.Now.ToString("yyyyMMddHHmmssffff");
        if (filesData != null && filesData.ContentLength > 0)
        {
            string directoryPath = Path.Combine(Server.MapPath("~/Images/Products/"), itemId);
            string filePath = Path.Combine(Server.MapPath("~/Images/Products/"), itemId, fileName+".jpeg");
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }               

            Image img = Image.FromStream(filesData.InputStream, true, true);

            img =  img.GetThumbnailImage(800, 600, () => false, IntPtr.Zero);
            img.Save(Path.ChangeExtension(filePath, "jpeg"));
            Image thumb = img.GetThumbnailImage(411, 274, () => false, IntPtr.Zero);
            thumb.Save(Path.ChangeExtension(filePath, "png"));
            ViewBag.MimeType = "image/pjpeg";
            TempData["ItemFilePath"] = "~/Images/Products/" + itemId +"/"+ fileName+".jpeg";
            TempData["ItemThumbnailFilePath"] = "~/Images/Products/" + itemId + "/" + fileName + ".png";
            TempData["ItemFileName"] = fileName + ".jpeg";
        }
        return Json(new
        {
            Success = true,
            Title = "Success",
            FileName = relativePath
        }, JsonRequestBehavior.AllowGet);

    }

谁能告诉我我的代码有什么问题?我正在设计图像尺寸必须很小的购物车。缩略图 (png) 的大小也增加了 200kb

最终尺寸很可能会增加,因为您没有在 img.Save() 方法上传递图像格式。

你应该改变

img.Save(Path.ChangeExtension(filePath, "jpeg"));

img.Save(Path.ChangeExtension(filePath, "jpeg"), ImageFormat.Jpeg);

png 图像相同(ImageFormat.Png)