Is it possible to save an array of WriteableBitmap to a file on disk as a whole, and retrieve it as a whole too?
-
WinRT, WPF, Silverlight?Denis– Denis2013-05-28 19:23:22 +00:00Commented May 28, 2013 at 19:23
-
@Denis I am interested in WinRT but also WPF if you know about.P5music– P5music2013-05-29 07:42:14 +00:00Commented May 29, 2013 at 7:42
2 Answers
You need to encode the output from WriteableBitmap into a recognised image format such as PNG or JPG before saving, otherwise it's just bytes in a file. Have a look at ImageTools (http://imagetools.codeplex.com/ ) which supports PNG, JPG, BMP and GIF formats. There's an example on saving an image to a file at http://imagetools.codeplex.com/wikipage?title=Write%20the%20content%20of%20a%20canvas%20to%20a%20file&referringTitle=Home.
Comments
You can retrieve a byte array from WritableBitmap. And that array can be saved and read to file. Something like this;
WritableBitmap[] bitmaps;
// Compute total size of bitmaps in bytes + size of metadata headers
int totalSize = bitmaps.Sum(b => b.BackBufferStride * b.Height) + bitmaps.Length * 4;
var bitmapData = new byte[totalSize];
for (int i = 0, offset = 0; i < bitmaps.Length; i++)
{
bitmaps[i].Lock();
// Apppend header with bitmap size
int size = bitmaps[i].BackBufferStride * bitmaps[i].Height;
byte[] sizeBytes = BitConverter.GetBytes(size);
Buffer.BlockCopy(sizeBytes, 0, bitmapData, offset, 4);
offset += 4;
// Append bitmap content
Marshal.Copy(bitmaps[i].BackBuffer, bitmapData, offset, size);
offset += size;
bitmaps[i].Unlock();
}
// Save bitmapDat to file.
And similar for reading from file.
UPD. Added headers with sizes of bitmaps. Without them it would be difficult to read separate bitmaps from single byte array.