C# code for efficiently reducing the size of a JPEG image based on the quality threshold.

Usecase

If you remember, I posted a blog post last week on "Convert base64 string to image and showing in a table using Angular Pipe." In this blog post, I used a byte array to show the table through angular code.

Convert base64 string to image and show in a table using Angular Pipe
Parameterize Pipe to transform the base64array string to a base64 image

Accidently, I got a performance issue on the API side because the API call is too slow when I am using large files. QA engineers also reported it.

How do I fix the above issue?

🚧
I used image-reducing size based on quality drop using the c# code. I assumed the image view was only in a table column in a small space.

Working on the problem

I used the below method to reduce the size of the image quality.

Explanation

Decreasing the size of a JPEG image by increasing the compression will also decrease the quality of the image. I reduced the size of a JPEG image using the Image class.

By decreasing the jpegQuality (which should be 0-100), you can increase the compression at the cost of lower image quality. See the Encoder.Quality field for more information.

Here is an example where you can see how jpegQuality affects image quality.

# JPEG QUALITY RATIO IMAGE SIZE
1 20 4.99 kb
2 50 8.28 kb
3 80 12.9 kb


This is how I fixed the performance issue reported by QA engineers. I hope you will learn something from my mistaken issue.

🚧
Keep touching my diaries. Learn something new. Comment your thoughts and share the content.

Thank you.!
Have an incredible coding journey.
See you soon. ✌✌✌

Share this post