Reputation: 3337
Is it possible to save an array of WriteableBitmap
to a file on disk as a whole, and retrieve it as a whole too?
Upvotes: 0
Views: 1099
Reputation: 486
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.
Upvotes: 0
Reputation: 146
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.
Upvotes: 2