我正在使用一个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
发布于 2018-02-25 01:10:15
最终的大小很可能会增加,因为您没有在img.Save()方法上传递图像格式。
你应该改变
img.Save(Path.ChangeExtension(filePath, "jpeg"));至
img.Save(Path.ChangeExtension(filePath, "jpeg"), ImageFormat.Jpeg);png图像也是如此(ImageFormat.Png)
https://stackoverflow.com/questions/48965266
复制相似问题