2

I'm using asp.net mvc 4 to upload images on my server. I'm using WebImage class to resize my images before uploading.

But I'm now sure why but my image file size is getting bigger after upload which shouldn't be happening because I'm resizing the original image to a smaller size before uploading.

Here is my code for uploading:

    [HttpPost]
    public ActionResult FlatImageOne(HttpPostedFileBase file, int getFlId)
    {
        if (file != null && file.ContentLength > 0)
        {
            string picName = getFlId.ToString() + "-0";
            WebImage img = new WebImage(file.InputStream);
            string picExt = Path.GetExtension(file.FileName);
            if (picExt == ".jpg" || picExt == ".gif" || picExt == ".jpeg" || picExt == ".png")
            {
                picExt = "PNG";
                string path = System.IO.Path.Combine(Server.MapPath("~/Images/Flats/"), picName);
                var img_resized = img.Resize(721, 482, false, false);
                img_resized.Save(path, picExt);
                return RedirectToAction("FlatImageOne", new { FlId = getFlId });
            }
            else
            {
                return RedirectToAction("FlatImageOne", new { FlId = getFlId });
            }
        }
        else
        {
            return RedirectToAction("FlatImageOne", new { FlId = getFlId });
        }
    }

How can I solve this issue?

halfer
  • 19,824
  • 17
  • 99
  • 186
Shihan Khan
  • 2,180
  • 4
  • 34
  • 67
  • 1
    what extension you have before? You save your Image in PNG and PNG is a format that don't looses quality. But JPEG compress your file with quality looses. So if you uploding jpeg and then save it like png it should (in general) be bigger. That could be even if you resize it to smaller once. – teo van kot Jan 13 '16 at 15:12
  • For now, I'm giving a png image to upload. But that doesn't make any sense, since I'm resizing it before upload, file size should be decreased but instead it increased. – Shihan Khan Jan 13 '16 at 15:15
  • you know, it just my gess, but could be that you should use `System.Drawing.Image` for resize purpuse check [this](http://stackoverflow.com/q/4181347/1849444) – teo van kot Jan 13 '16 at 15:31
  • There are different ways to encode a PNG file. If your image started out as an 8-bit or 24-bit PNG, then you saved it as a 32-bit, it's going to be bigger. – Tieson T. Nov 21 '20 at 20:15

0 Answers0