Click here to Skip to main content
15,910,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me about this thing. How can I transform/convert a big size photo(e.g. 3MB) to small size(e.g. 980KB)? using c#.net
Posted
Comments
Sergey Alexandrovich Kryukov 23-May-11 1:05am    
This is not quite clear. You can specify new size in pixels, not in image file size. I explain it in my solution. Do you want to reduce the image size as well (otherwise you cannot solve the problem at all)?
--SA
Matt Jun 23-May-11 1:07am    
Is specifying new pixels can make the image size small?
Sergey Alexandrovich Kryukov 23-May-11 3:33am    
What do you mean "image size"? File size? Naturally, the less the bitmap size, the less the file size, but you cannot guarantee any certain file size anyway.
--SA

This is called re-sampling.

Now, as you're discussing file size and not bitmap size, it means you're talking about compressed image file formats. You cannot calculate the conversion parameter by the desired output file size. This size depends on many parameters: type and parameters of compression format and level of complexity of the image itself. It is impossible to predict the resulting size of the image as a result of compression before the compression itself is done.

I suggest you get some experience with re-sampling of real-life bitmaps to certain sizes and save the results to file to get an idea of output file size.

To do re-sampling, you can use the class System.Windows.Media.Imaging.TransformedBitmap.
See http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.transformedbitmap.aspx[^]. See the code sample on this page and the usage of the property System.Windows.Media.Imaging.TransformedBitmap.Transform. You need to assign the different instance of the transform class to this property to get scale down the image.

Use it with the transform class System.Windows.Media.ScaleTransform, see http://msdn.microsoft.com/en-us/library/system.windows.media.scaletransform.aspx[^].

Nothing can give you full guarantee of the specified resulting file size on output.

—SA
 
Share this answer
 
v4
Comments
Matt Jun 23-May-11 1:10am    
Thanks.Now, I have an idea.
Sergey Alexandrovich Kryukov 23-May-11 1:13am    
I hope it's enough for you to operate your images, but feel free to ask for help when and if you face a problem.
Thanks for accepting this solution.

Good luck,
--SA
Christoph Keller 23-May-11 3:24am    
Thank you for sharing this! Never heard of it and I will definitely try this out next time!
my 5+!
Sergey Alexandrovich Kryukov 23-May-11 3:38am    
Thank you, and you're welcome. I just re-voted 5 to your answer as well. I thought the question was about WPF (maybe it was in the first version of the question?) but for ASP.NET any imaging library can be used, it does not matter. (Using System.Drawing with WPF would be possible with extra hassle (many inquirers did not know what to do.) So, if you receive my e-mail notification where I down-voted your answer, please disregard it, I up-voted it by 5.
--SA
Hello,

Another way would be that you use the Image object of the System.Drawing namespace.
You could load the image and process the image by just playing with the EncoderParameters and save it to a file or a stream:

C#
/// <summary>
/// Method for getting the encoder info out of a mime type.
/// </summary>
/// <param name="mimeType">The mime type to search for.</param>
/// <returns>The <see cref="ImageCodecInfo" /> for the supplied mime type. If the mime type is not found, null is returned.</returns>
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
    int j;
    ImageCodecInfo[] encoders;
    encoders = ImageCodecInfo.GetImageEncoders();
    for (j = 0; j < encoders.Length; ++j)
    {
        if (encoders[j].MimeType == mimeType)
            return encoders[j];
    }
    return null;
}
/// <summary>
/// Image processing reduces the file quality to level 25 (of 100) and stores the new image.
/// </summary>
/// <param name="inputFilename">The image to process.</param>
/// <param name="outputFilename">Filename, where the processed image should be saved.</param>
public void ProcessImage(string inputFilename, string outputFilename)
{
    // Load the image from the file.
    Image inputImage = Image.FromFile(inputFilename);
    // Get an ImageCodecInfo object that represents the JPEG codec.
    ImageCodecInfo imageCodecInfo = GetEncoderInfo("image/jpeg");
    // Get the image encoder Guid for the quality property.
    Encoder qualityParamIdentifier = Encoder.Quality;
    // Initialize the encoder parameters array.
    EncoderParameters encoderParameters = new EncoderParameters(1);
    // Define a new EncoderParameter for the quality parameter and set it to quality level 25 (of 100)
    EncoderParameter qualityParam = new EncoderParameter(qualityParamIdentifier, 25L);
    // Add the quality parameter to the encoder.
    encoderParameters.Param[0] = qualityParam;
    // Save the bitmap.
    inputImage.Save(outputFilename, imageCodecInfo, encoderParameters);
}




Here are more informations about the Image.Save and the EncoderParameters defining the image quality:
http://msdn.microsoft.com/en-us/library/ytz20d80.aspx[^].


Hope this helps.

Best regards, have a nice day and happy coding,
Stops
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-May-11 3:29am    
Good sample, my 5.
--SA
Christoph Keller 23-May-11 5:01am    
Thank you! :)

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