简体   繁体   English

保存和加载图像后使用LockBits / UnlockBits时数据损坏

[英]Corrupted data when using LockBits/UnlockBits after saving and loading images

I'm writing a steganography program which supports multiple file types. 我正在编写一个支持多种文件类型的隐写术程序。 There is a working implementation for WAV files and then one for image files, which is currently not working. 对于WAV文件,有一个有效的实现,对于图像文件,有一个有效的实现,当前不起作用。

The image carrier is basically a wrapper around the .NET classes Bitmap and Image. 图像载体基本上是.NET类Bitmap和Image的包装。 It uses LockBits/UnlockBits to access the array of bytes contained in images. 它使用LockBits / UnlockBits访问图像中包含的字节数组。 When the program is finished making changes to the byte array, it will copy it back into the Bitmap. 程序完成对字节数组的更改后,会将其复制回位图。

When the data has been copied and the Bitmap has been unlocked, I save it using: 复制数据并解锁位图后,我使用以下方法进行保存:

image.Save(destination, ImageFormat.Png);

The next time I load the Image, I would expect that the byte array I saved earlier is identical to the one that I am going to extract from the saved Image (since PNG is lossless). 下次加载图像时,我希望之前保存的字节数组与我要从保存的图像提取的字节数组相同(因为PNG无损)。 However, this is not always the case. 然而,这并非总是如此。

The byte arrays are ALMOST identical, but SOME of the bytes with a value of 1 have been changed to 0. Other than this there is not really any pattern to it, seeing as it appears to happen to all channels. 字节数组几乎是相同的,但有些值为1的字节已更改为0。除此之外,实际上并没有任何模式,因为它似乎出现在所有通道上。

Is there something about Image.Save or the PNG format that I have missed? 我错过了有关Image.Save或PNG格式的内容吗?

edit: When embedding the exact same data, it works for some images but not for all, even if they share the same attributes (both 24 bpp and PNG). 编辑:嵌入完全相同的数据时,即使它们共享相同的属性(24 bpp和PNG),它也适用于某些图像,但不适用于所有图像。

When saving an image you can specify different compression algorithms. 保存图像时,可以指定不同的压缩算法。 The default one might produce this undesired effect. 默认值可能会产生这种不良效果。 Try another one. 尝试另一个。

See: 看到:
- System.Drawing.Imaging.Encoder Compression -System.Drawing.Imaging.Encoder压缩
- System.Drawing.Imaging.EncoderValue Enumeration -System.Drawing.Imaging.EncoderValue枚举
- Image.Save Method (String, ImageCodecInfo, EncoderParameters) -Image.Save方法(字符串,ImageCodecInfo,EncoderParameters)

Don't confuse these compression algorithms with the image quality. 不要将这些压缩算法与图像质量混淆。 Image quality should not affect PNGs, as they use lossless compression. 图像质量不应影响PNG,因为它们使用无损压缩。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM