views:

622

answers:

1

I am supposed to write a program that gets some PNG images from the user, does some simple edits like rotation and saves them inside a JAR file so that it can use the images as resources. The problem is when I open, say an 80kb image and then save it with C#, I get an image with the same quality but for 130kb space. And because it has to go inside a J2ME jar file I really need lower sizes, at least the original size. I tried the code below but later found out that it only works for Jpeg images.

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                int j = 0;
                for (j = 0; j < codecs.Length; j++)
                {
                    if (codecs[j].MimeType == "image/png") break;
                }
                EncoderParameter ratio = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 10L);
                EncoderParameters CodecParams = new EncoderParameters(1);
                CodecParams.Param[0] = ratio;

                Image im;
                im = pictureBox1.Image;
                im.Save(address , codecs[j], CodecParams);

This is where the image is loaded to a picture box:

private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string address = openFileDialog1.FileName;
                address.Replace("\\", "\\\\");
                Image im = Image.FromFile(address);
                pictureBox1.Image = im;
            }
        }

And this is where it's being saved back with no edits:

private void generateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {

                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                int j = 0;
                for (j = 0; j < codecs.Length; j++)
                {
                    if (codecs[j].MimeType == "image/png") break;
                }
                EncoderParameter ratio = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 10L);
                EncoderParameters CodecParams = new EncoderParameters(1);
                CodecParams.Param[0] = ratio;

                string address = folderBrowserDialog1.SelectedPath;
                address = address + "\\";
                address.Replace("\\", "\\\\");

                Image im;
                im = pictureBox1.Image;               
                im.Save(address + imageFileNames[1], codecs[j], CodecParams);

Note: imageFileNames[] is just a string array that has some of the the file names to which the images must be saved with.

Any ideas will be appreciated and thanks in advance.

+1  A: 

I have taken 4 different PNGs, ranging in sizes from 2KB to 2.6MB. Using your code, I load them up and save them out. I do not modify the image by rotating or flipping. All 4 PNGs, when re-saved, have exactly the same size.

In addition, I have taken the 2.6MB PNG, opened it in Photoshop and saved two copies of it, one interlaced (reduced to 2.06MB), one non-interlaced (reduced to 1.7MB.) I then took each of those, ran them through your code and saved them. The resulting sizes of both were back to the original 2.6MB.

So, my guess is that the original images were created with a piece of software (like photoshop) and that piece of software has an optimized compression algorithm it is using for the PNG spec that .NET does not employ.

I've messed around with the EncoderParameter for Compression, but it seems to have no impact.

Jacob G
Yeb, your right. The images were exported by the 3dsMax jsr exporter.But do you know a way to compress the PNG or lower it's quality so I could acheive a similar result to the exporter?
Auxiliary
This looks promising: http://benhollis.net/software/pnggauntlet/ ... It wraps this PNGOUT from this page: http://advsys.net/ken/utils.htm
Jacob G
As does this: http://optipng.sourceforge.net/
Jacob G
Thanks for your time, I think you gave me my answers.
Auxiliary