2

I have compress method which is uses SixLabors.ImageSharp. When i compress a image on that method, its getting more size before then i upload it. The image i upload is 2,03 mb enter image description here

and when its come out from the compression method, its getting like that 4,54 mb enter image description here

and here it's my compression method :

public async Task<FileRepo> FileUploadToDatabase(List<IFormFile> files)
{
    foreach (var file in files)
    {
        var fileName = Path.GetFileNameWithoutExtension(file.FileName);
        var fileExtension = Path.GetExtension(file.FileName);
        using var image = Image.Load(file.OpenReadStream());
        IImageEncoder imageEncoderForJpeg = new JpegEncoder()
        {
            Quality = 80,
        };
        IImageEncoder imageEncoderForPng = new PngEncoder()
        {
            CompressionLevel = PngCompressionLevel.Level9,
        };
        _fileRepo = new FileRepo
        {
            FileName = fileName,
            FileExtension = fileExtension,
            FileType = file.ContentType,
            CreatedDate = DateTime.Now
        };
        using (var ms = new MemoryStream())
        {
            if (fileExtension == ".png")
            {
                image.Save(ms, imageEncoderForPng);
            }
            if (fileExtension == ".JPEG" || fileExtension == ".jpg")
            {
                image.Save(ms, imageEncoderForJpeg);
            }
            await file.CopyToAsync(ms);
            _fileRepo.FileData = ms.ToArray();
        }
    }
    return _fileRepo;
}

i dont know whats wrong with that method , it should be less size then first one right ? let me know if that question is duplicate.

BerkGarip
  • 534
  • 5
  • 18
  • So much unnecessary code here. `Load` can tell you the `IImageFormat` (*You should never trust file extensions*). You're probably saving 24bit pngs as 32bit also. – James South Jul 27 '21 at 15:33
  • is this a common thought ? because i wanna believe you. its not working in a way exactly what i wanted. i have look around to find some examples but i couldnt find. its seems like i wont compress them for now @JamesSouth . and as u said,sometimes its returnig .jpeg files as .png. – BerkGarip Jul 27 '21 at 15:36
  • Png will always be bigger than Jpeg when encoding photographic style images. Jpeg is specifically designed to store the data in a manner that works well with the human eye. Png does much better when working with areas of flat color. https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Image.html#SixLabors_ImageSharp_Image_Load_SixLabors_ImageSharp_Configuration_System_IO_Stream_SixLabors_ImageSharp_Formats_IImageFormat__ – James South Jul 27 '21 at 15:42
  • @JamesSouth this is the first time im doing that process. im doing that compression for product photos which is gonna display on the e-commerce website. my page is downloading 25 mb (40 product Photos,10 category photos) data because of those pics and its making the site slower. i wanna reduce that as long as i can. which way is much better for your experience ? – BerkGarip Jul 27 '21 at 15:48
  • 1
    If it's ASP.NET Core Use ImageSharp.Web middleware to produce thumbnails. https://docs.sixlabors.com/articles/imagesharp.web Only show full size images when required. – James South Jul 28 '21 at 00:11

0 Answers0