1

I am uploading an image in c# using a fileapi plugin. My actual file size is 60kb but after uploading the file size appears as 350kb on the server. Why is this happening? Here is my code for saving an image:

    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);

    }

Can anyone tell me what is the problem with my code? I am designing shopping cart in which image size must be small. The thumbnail image (png) also taking more size 200kb

Prabu
  • 4,097
  • 5
  • 45
  • 66
  • 2
    Well you're not saving the original stream - you're converting it using `GetThumbnailImage`. To get the original data size, just save the original stream - check that that works. Then if you want to look into resizing etc, you can entirely separate that aspect from the upload aspect. – Jon Skeet Feb 24 '18 at 17:00
  • i already tried.also same issue with orignal stream. – Ammar Muhi ud din Feb 24 '18 at 17:08
  • Please confirm that you're actually asking how to apply image compression... If so, it's a dupe: https://stackoverflow.com/questions/1484759/quality-of-a-saved-jpg-in-c-sharp – spender Feb 24 '18 at 17:08
  • 1
    Please show the code that uses the original stream. I'd be astonished to see that really giving a problem. – Jon Skeet Feb 24 '18 at 17:15
  • As an aside, I'd *strongly* recommend against using `DateTime.Now` for this - use `DateTime.UtcNow` to avoid confusion due to time zones. – Jon Skeet Feb 24 '18 at 17:15

1 Answers1

0

The final size is most likely increasing because you're not passing your image format on img.Save() method.

You should change

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

to

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

Same for the png image (ImageFormat.Png)

Zair Henrique
  • 610
  • 1
  • 6
  • 20