Click here to Skip to main content
15,117,268 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
Hi all,

I need to change the size of many jpg images to be 3.8 kilobytes.

I tried the EncoderParameter epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, val);
But I am not able to make the image reach a specific size in Kb.

Can anyone help?
Updated 27-Mar-11 7:11am
Piccadilly Yum Yum 27-Mar-11 11:30am
size of encoded image depends by its content, same quality on diffrent image leads to diffrent sizes. so algorithm is not yet calibrated on resulting image size..
Mohammad Al Hoss 27-Mar-11 11:44am
I tried to save the image with percent value to 50 then see what is the size in bytes, then based on the new image size I calculate the new percent but also not working well.

Is there any other way to do this, other methods or something.
Piccadilly Yum Yum 27-Mar-11 11:50am
you should study jpeg compression to understant if a way exist to you. look at there :
Mohammad Al Hoss 27-Mar-11 11:52am
Steve Wellens 27-Mar-11 11:42am
Do you mean 3.8k on disk?
Jpeg images are not compressed until they are saved.
The size is dependent on the image content and compression level.
A klunky, crude way would be to save two temporary images with a high and a low compression. Check their sizes and interpolate what the compression level should be.
Mohammad Al Hoss 27-Mar-11 11:46am
here is the code i am using
Int64 RequiredLength = (Int64) (3.8 * 1024);

//EncoderParameter epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (int)numQual.Value);
int val = 50;
EncoderParameter epQuality = null;
//epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, val);
epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, val);

// Store the quality parameter in the list of encoder parameters
EncoderParameters epParameters = new EncoderParameters(1);
epParameters.Param[0] = epQuality;

// Create a new Image object from the current file
Image newImage = null;
newImage = Image.FromFile(strFile);
// Get the file information again, this time we want to find out the extension
FileInfo fiPicture = new FileInfo(strFile);
// Save the new file at the selected path with the specified encoder parameters, and reuse the same file name
string savepath = "TempResize\\" + fiPicture.Name;
newImage.Save(savepath, iciJpegCodec, epParameters);
FileInfo ResizedInfo = new FileInfo(savepath);
Int64 ResizedInBytes = ResizedInfo.Length;
//double somrthing = (3.8 * val) / ResizedInKb;
Int64 RequiredVal = (RequiredLength * val) / ResizedInBytes;
epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, RequiredVal);
// Create a new Image object from the current file
Image ResizedImage = null;
ResizedImage = Image.FromFile(strFile);
epParameters.Param[0] = epQuality;
ResizedImage.Save("TempResize\\New\\" + fiPicture.Name, iciJpegCodec, epParameters);
Dalek Dave 27-Mar-11 12:12pm
Edited for Readability.
Sergey Alexandrovich Kryukov 27-Mar-11 16:30pm
No way. Just forget about it.

You cannot specify the resulting binary size of the JPEG. The only way to do what you want would be to incrementally reduce the Quality value and resave the image over and over again until it meets your specification. The problem with this is that you could reduce the Quality so low as to make the image unrecognizable.
Sergey Alexandrovich Kryukov 27-Mar-11 16:28pm
Correct, a 5.
Upload the image to PAINT and reduce it, then save it as the smaller file.
Or, email it to yourself, that also gives a reduction option.

Picture quality is the price you pay though.

Another hint to to trim away the edges of the unimportant material or background.
Sergey Alexandrovich Kryukov 27-Mar-11 16:29pm
Dalek, whatever you do, you cannot guarantee you meet the requirement of the file size.
(I did not vote)
Hello, I need a sample code to reduce the size but actual quality like facebook. If any SDK, took or code..? any body can help me.?

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