question

osyris-3187 avatar image
0 Votes"
osyris-3187 asked Viorel-1 commented

Best way to Compress Image in Asp net core

I want to Compress all uploaded image before it gets Uploaded on the websites disk.
I have allot of small thumbnail sized images (shopping page) on a page so the images need to be at there minimal size while stil be in good quality so it will not take that long to load a page

How I upload the images right now:

                         string name = Guid.NewGuid().ToString() + "_" + image.FileName;
                         string filepath = Path.Combine(root, name);
    
                         using var stream = System.IO.File.Create(filepath);
                         await image.CopyToAsync(stream);




dotnet-aspnet-core-generaldotnet-aspnet-generaldotnet-aspnet-core-webapi
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


If you do not need the original large image, consider resizing the images in browser before uploading: https://www.bing.com/search?q=resize+image+before+upload+javascript.


1 Vote 1 ·

1 Answer

JaliyaUdagedara avatar image
0 Votes"
JaliyaUdagedara answered JaliyaUdagedara edited

You can do something like the below,

 Bitmap resizedImage = new(image, new Size(256, 256));
 using var stream = new MemoryStream();
 resizedImage.Save(stream, ImageFormat.Jpeg);
 byte[] bytes = stream.ToArray();

 // Write bytes to your desired location
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.