Is it possible to save an array of WriteableBitmap
to a file on disk as a whole, and retrieve it as a whole too?
Asked
Active
Viewed 1,091 times
0

John Willemse
- 6,608
- 7
- 31
- 45

P5music
- 3,197
- 2
- 32
- 81
-
WinRT, WPF, Silverlight? – Denis May 28 '13 at 19:23
-
@Denis I am interested in WinRT but also WPF if you know about. – P5music May 29 '13 at 07:42
2 Answers
2
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.

Aditya Patil
- 146
- 9
0
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.

OpenMinded
- 496
- 3
- 10