我使用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
答案 0 :(得分:0)
最终尺寸很可能会增加,因为您未在img.Save(Path.ChangeExtension(filePath, "jpeg"));
方法上传递图像格式。
你应该改变
img.Save(Path.ChangeExtension(filePath, "jpeg"), ImageFormat.Jpeg);
到
ImageFormat.Png)
png图像(col-md-6