Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a web application.I want to implement the following Functionality ,Hope You guys Help me:

While Uploading an image File,I want to do Automatically decrease the size of the photo to Exact 2 MB if the photo is > 2 MB Compromises with image quality,height and width.

How implement this guys in c#,Hope You Elaborate with Sample Code

Thanks in Advance...

What I have tried:

i tried lot but not reduced to exact 2Mb, reduce to non-predictable size < 2Mb
Updated 19-Jul-17 22:30pm
LLLLGGGG 19-Jul-17 13:27pm    
i tried lot but not reduced to exact 2Mb, and also changed the width,height

That is because it's impossible: you cannot reduce the file size and keep the width, height and quality the same! You can:
1. Reduce width/height
2. Change algorithm for lossy compression (does not necessarily imply a loss of quality, but it means that you need to upload the photo even if it's >2MB and save it again in a new image type) or increase the amount of compression that the algorithm uses (decreases quality and you still need to upload the photo even if it's >2MB)
3. Use a lossless compression after the uploading (GZip algorithm for instance).

You can't, for two reasons.
1) You can't change a file while or before it's uploaded - C# code only runs on the Server, not the client - and the server has no access to the data until the upload is complete.
2) If you could always make an image smaller without affecting the height, width, or quality, don't you think that the original image would already have done that? You may be able to reduce image size by changing the format in which you store it, provided you use a lossless compression format (PNG is, JPG isn't for example) but there is no guarantee it will get smaller (it may even get bigger for some images) and absolutely no guarantee that the resulting image size will be any specific value.
Share this answer
Member 13318210 19-Jul-17 23:24pm    
ok..I am Compromise with Quality of Image.Is there Anyway for reducing size by changing resolution?? if there, will you please elaborate with sample code

Thanx in Advance
I want to do Automatically decrease the size of the photo to Exact 2 MB if the photo is > 2 MB without changing the width,height and quality.

It is just impossible. width, height, quality and size of an image are linked, you can't change one without changing another.
While Uploading an image File,

You do nothing while uploading an image. You need to wait until the end of upload.
Automatically decrease the size of the photo to Exact 2 MB

Exact size is almost impossible to achieve.
Share this answer
You'll probably have to save the image as a JPG and check the file size, if it's too big save it again with a higher compression, check the size and so on. Code for changing compression ratio can be found here

How to: Set JPEG Compression Level | Microsoft Docs[^]

pseudocode would be something like

quality = 100
save image at quality
do while image size > 2MB and quality > 10
    quality = quality - 10
    save image at quality
end do

To preempt your next question, no I won't write it for you, there are loads of examples on how to upload images and save them, and I've shown you the code for how to save at a given quality, so google those samples, understand them, then put them together.
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