1

I have an InkCanvas drawing, which i convert to WriteableBitmap and set is as an ImageSource as follows:

MemoryStream ms = new MemoryStream();
await inkCanvas.InkPresenter.StrokeContainer.SaveAsync(ms.AsOutputStream());
ms.Position = 0;
WriteableBitmap wb = new WriteableBitmap((int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight);
wb.SetSource(ms.AsRandomAccessStream());
image.Source = wb;

That works fine.

Now i need to store the WriteableBitmap as a byte array in database in order to use it as image source of a GridView item. This is the method i use to convert the bitmap to byte[]:

using (Stream stream = ((WriteableBitmap)image.Source).PixelBuffer.AsStream())
{
    data = new byte[(uint)stream.Length];
    await stream.ReadAsync(data, 0, data.Length);
}

and this one to restore the bitmap form byte[]:

WriteableBitmap wb = new WriteableBitmap(200,200);
using (Stream stream = wb.PixelBuffer.AsStream())
{
    await stream.WriteAsync(data, 0, data.Length);
}

Now here are the screenshots describes how the image looks after retrieving from InkCanvas and after retrieving from byte[]:

enter image description here

Will be grateful if someone faced that issue and has a solution.

J.Doe
  • 326
  • 2
  • 14
  • Have you taken a look [at this thread](http://stackoverflow.com/q/35111635/2681948)? – Romasz Feb 19 '17 at 14:15
  • Yep, tried other methods, including those in the thread you provided. Results are different, sometimes it returns those dots, sometimes just a blank image and sometimes it throws an error. – J.Doe Feb 19 '17 at 14:44
  • 2
    How do you know the original bitmap was 200x200? – Raymond Chen Feb 19 '17 at 15:09
  • Raymond Chen, im so dumb, the size was incorrect. I had corrected it and it is working now! Post an answer, i will accept it. – J.Doe Feb 19 '17 at 15:15

0 Answers0