0

Is it possible to save an array of WriteableBitmap to a file on disk as a whole, and retrieve it as a whole too?

2
  • WinRT, WPF, Silverlight? Commented May 28, 2013 at 19:23
  • @Denis I am interested in WinRT but also WPF if you know about. Commented May 29, 2013 at 7:42

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.