从磁盘读取png文件并将其保存回来后,文件大小增加 [英] File size increases after reading png file from disk and saving it back

查看:153
本文介绍了从磁盘读取png文件并将其保存回来后,文件大小增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个小程序,用于从磁盘读取png文件,进行一些修改并将其保存回去.除了一个小问题,一切都运行顺利,将文件保存回磁盘后,文件大小总是增加,例如,一个27.1MB的文件将变为33.3MB.

I'm currently working on a small program to read png files from disk, do some modifications and save it back. Everything is running smoothly except for one small problem, after I saved the file back to disk, its size always increases, for example, a 27.1MB file will become 33.3MB.

经过一些调试后,我最终将其范围缩小到了我的阅读和保存代码.这是我当前正在使用的代码:

After some debugging I finally narrow it down to my reading and saving code. This is the code I'm currently using:

Bitmap img = new Bitmap(<path to file>);
//omitted
img.Save(<path to new file>, ImageFormat.Png);

无论是否做任何修改,我都已进行了验证,仅读取和保存图像就会导致其大小发生变化.此外,如果我用Paint打开保存的文件并从那里保存,文件将缩小到原始大小.

I've verified no matter if I do or do not make any modification, simply reading and saving the image will cause it size to change. Furthermore, if I opened the saved file with Paint and save from there, the file will shrink back to original size.

如何在不更改图像大小的情况下读取并保存图像?

How do I read and save the image without changing its size?

推荐答案

您是否尝试过使用Endoder.ColorDepth字段? PNG还支持透明度,并且可能会保存一些图像不需要的信息.

Have you tried playing with the Endoder.ColorDepth field? PNG also supports transparency and might be saving some information not needed by your image.

ImageCodecInfo pngCodec = ImageCodecInfo.GetImageEncoders().Where(codec => codec.FormatID.Equals(ImageFormat.Png.Guid)).FirstOrDefault();
if (pngCodec != null)
{
    EncoderParameters parameters = new EncoderParameters();
    parameters.Param[0] = new EncoderParameter(Encoder.ColorDepth, 24); //8, 16, 24, 32 (base on your format)
    image.Save(stream, pngCodec, parameters);
}

此处的其他信息: 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆