本文介绍了C# - 如何更改PNG品质或色深的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该写一个程序,它从用户得到一些PNG图片,做一些简单的编辑像旋转并保存一个JAR文件中,这样它可以使用图像作为资源。问题是,当我打开,说,一个80KB的图像,然后用C#保存它,我得到的图像具有相同的质量,但对于130KB的空间。而且因为它有走了J2ME jar文件里面,我真的需要更低的大小,至少原来的大小。我想下面的代码,但后来发现,它仅适用于JPEG图像。

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);

请注意:imageFileNames []仅仅是有一些文件名的字符串数组,其中图像必须被保存。

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.

推荐答案

我已经采取4种不同的PNG图像不等,尺寸从2KB到2.6MB。用你的代码,我装载它们,并将它们保存了。我不通过旋转或翻转修改图像。在Photoshop中所有4 png格式,重新保存-时,具有完全相同的尺寸。

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.

另外,我已经采取了2.6MB PNG,打开它并保存的两个副本它一隔行扫描(减少到2.06MB),一个非隔行扫描(降低到1.7MB)。然后我把每一个这些,跑他们通过你的代码并保存它们。由此产生的两种尺寸都回到原来的2.6MB。

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.

所以,我的猜测是,原始图像进行了一块软件创建的(如Photoshop)和一款软件都有它使用针对.NET不采用PNG规范优化的压缩算法。

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.

我与周围的EncoderParameter的压缩搞砸,但它似乎也没有影响。

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

这篇关于C# - 如何更改PNG品质或色深的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:48