C# - 如何更改 PNG 质量或颜色深度

C# - How to change PNG quality or color depth(C# - 如何更改 PNG 质量或颜色深度)
本文介绍了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 不等.使用您的代码,我将它们加载并保存出来.我不会通过旋转或翻转来修改图像.重新保存后,所有 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,在 Photoshop 中打开并保存了两份,一份是交错的(减少到 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 质量或颜色深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)