简体   繁体   English

为什么将图像保存在C#位图上的文件大小与原始文件不同?

[英]Why does saving image on C# Bitmap give different file size than original?

I am currently working on a project which modify some images and save it again. 我目前正在修改一个图像并再次保存的项目。 However, I have noticed that sometimes the resulted image is drastically different than the original one in terms of file size in bytes. 但是,我注意到有时生成的图像在字节大小方面与原始图像大不相同。 Even if the modifications were very small such as changing colour on a single pixel. 即使修改很小,例如在单个像素上更改颜色。 So I decided to do this experiment where I load an image and save it again immediately to new file without any changes (using C# Bitmap class). 因此,我决定进行此实验,在其中加载图像并立即将其再次保存到新文件中,而无需进行任何更改(使用C#Bitmap类)。 I was expecting to get the same size in both files but unfortunately the actual sizes were not identical. 我期望两个文件的大小相同,但不幸的是实际大小并不相同。 Here is my code: 这是我的代码:

String originalImagePath = "image.png";
String savedImagePath = "image2.png";
Bitmap image = new Bitmap(originalImagePath);
image.Save(savedImagePath);

Assert.AreEqual(new FileInfo(originalImagePath).Length, new FileInfo(savedImagePath).Length);

Assert.AreEqual failed. 
Expected:<218144>.Actual:<344264>.

Should both files have the same sizes? 两个文件的大小应该相同吗? If no, then what I am missing here? 如果没有,那么我在这里想念的是什么?
And how can I generate identical file? 以及如何生成相同的文件?
Here is the image that I am using: PNG Image 这是我正在使用的图像: PNG图像

There really isn't anything strange about this. 真的没有什么奇怪的。 You're saving a PNG file, which only really defines how the file is supposed to be decoded, but not how to do the actual compression. 您正在保存一个PNG文件,该文件只能真正定义应该如何解码文件,而不能定义实际压缩方式。 Different applications can produce vastly different file sizes, just by the virtue of having a different compression tables. 仅依靠具有不同的压缩表,不同的应用程序就可以产生截然不同的文件大小。 There's usually also some tweaking you can do (eg limit the palette of the file, or picking between coding/decoding speed and file size). 通常,您还可以进行一些调整(例如,限制文件的调色板,或者在编码/解码速度和文件大小之间进行选择)。

This is seen very clearly by using a tool such as PNG Gauntlet, which takes a PNG file on input, and produces a pixel-by-pixel identical image, just with a vastly reduced file size (usually around 30% less space, but I've seen results up to 70% smaller). 使用诸如PNG Gauntlet之类的工具可以很清楚地看到这一点,该工具在输入时获取一个PNG文件,并生成一个逐像素的相同图像,但文件大小却大大减小了(通常少30%左右,但我的结果缩小了70%)。

And of course, in the end, even if everything else is equal, a tiny change in the bitmap can result in a significant file increase anyway. 当然,最后,即使其他所有条件都一样,位图的微小变化也可能导致文件数量显着增加。 Maybe you've broken some pattern that was especially easy to compress. 也许您破坏了一些特别容易压缩的模式。 Maybe you can no longer fit in a 8-bit palette. 也许您不再适合8位调色板。 Maybe you've added transparent channels. 也许您已经添加了透明渠道。 There's plenty more. 还有更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在保存图像时C#中的位图会扩展图像的文件大小? - Why does the Bitmap in C# expand an image's file size when saving it? 为什么将图像写入流中与直接保存到文件中相比,给我带来不同的结果? - Why does writing an image to a stream give me a different result than directly saving to a file? C#上传图像并保存为位图增加图像的大小 - C# Upload image and saving as Bitmap Increase size of the image 图像调整大小导致文件大小比原始C#大 - Image resize results in massively larger file size than original C# 为什么 C# 中的签名算法给出的结果与 Javascript 中的不同 - Why does signing algorithm in C# give different result than the one in Javascript 位图大于原始图像 - Bitmap larger than original image Xamarin Forms - 根据原始源文件大小在C#中设置图像大小 - Xamarin Forms - set an image size in C# based on the original source file size c#图像大小保存后增加 - c# Image size increases after saving it 使用FileStream或Simple Bitmap在c#中更新后未保存图像 - Image is not saving after updating, in c# using FileStream or Simple Bitmap 使用 C# bitmap 更有效地将图像保存到磁盘 - Saving image to disk with C# bitmap more efficiently
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM