我试图隐藏PNG图像中的数据,如下所示:
// Creates a new empty image with the pre-defined palette
BitmapSource image = BitmapSource.Create(
width,
height,
96,
96,
PixelFormats.Bgr24,
myPalette,
imageData,
stride);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Interlace = PngInterlaceOption.On;
BitmapFrame frame = BitmapFrame.Create(image);
encoder.Frames.Add(frame);
//estimate PNG file size using the amount of data being saved
MemoryStream arrayStream = new MemoryStream(imageData.Length);
encoder.Save(arrayStream);
其中 imageData 是我隐藏在 PNG 图像中的数据。这是我如何解码它:
Stream encodedImageStream = new MemoryStream(imageData, 0, imageDataSize);
PngBitmapDecoder decoder = new PngBitmapDecoder(encodedImageStream, BitmapCreateOptions.None, BitmapCacheOption.Default);
BitmapFrame bitmapSource = decoder.Frames[0];
//align on the rhs boundary
int stride = ((bitmapSource.PixelWidth + 1) * bytesPerPixel) & ~3;
byte[] pixels = new byte[bitmapSource.PixelHeight * stride];
bitmapSource.CopyPixels(pixels, stride, 0);
问题是编码器似乎正在将图像的 PixelFormat 从 Bgr24 更改为 Bgr32,在解码所有像素值在编码之前设置为 0 的图像后,我得到一个像素值的图像 - [0,0,0,255,0 ,0,0,255,...] 这表明编码器为图像添加了透明度,我希望它保持格式相同,请帮助