Click here to Skip to main content
15,883,127 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to reduce the image size or compress the image, before the upload has started. I want to handle this in OnClientUploadStarted event of asyncfileupload.


What I have tried:

Didnt find any solution soo far.
Updated 12-Apr-17 22:10pm
Patrice T 6-Apr-17 17:12pm    
Do you have a question ?
Use Improve question to update your question.
Nick_3141592654 12-Apr-17 18:42pm    
Image format? Target compression? Image quality constraint(s)? Do you have magical skills to bring to bear?
Member 13109973 13-Apr-17 1:37am    
retain the same image format. Fix the compression to 50% of the image. No quality constraint. Do you use whatsapp? In whatsapp they compress the image.
And check this link or its image compression. I want exactly the same thing but with asyncfileupload. Any leads will be much appriciated.

1 solution

OK, so you are talking about re-sampling the image. For example, starting with a 4096x2048 PNG (or whatever format) you're wanting to convert this to, say, 2048x1024 which preserves the aspect ratio but compresses the size by a factor of 4 (2 in each dimension).

You can't in general re-sample without having some impact on quality, because there's a necessary interpolation which introduces some blur between nearby pixels. However for most practical purposes this is OK. Don't worry about this because the algos have been worked out for many years and you aren't going to do better than using existing "off the shelf" ones.

There's a nice C# example project on this topic elsewhere on Code Project, and another that goes into more depth on the choice of re-sampling algorithm here.

Finally, you mentioned a 50% compression. If you mean 50% in total image size this requires a 1/SQRT(2) = 0.707 reduction in the X and Y dimensions. For example, an original 4096 X 2048 image is going to need to reduce to 2896.3 X 1448.15: In that case you will need to round to an integer number of pixels such as 2896 X 1448. Interpolation will generally be more optimum (and with computational optimisations more likely achieved) if you stick to reductions in each dimension by powers of 2, but in that case the minimum compression achieved will be 2x2 => 4 => 25% reduction in size.

I hope this helps.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900