'Converting String to Stream

So I am trying to serialize and deserialize an IReadOnlyList<InkStroke> from Windows.UI.Input.Inking for my UWP app, and I have used the following code to serialize the data:

var stream = new InMemoryRandomAccessStream();
await canvas.InkPresenter.StrokeContainer.SaveAsync(stream);
await stream.FlushAsync();

var buffer = new byte[stream.Size];
await stream.AsStream().ReadAsync(buffer, 0, buffer.Length);
return Convert.ToBase64String(buffer);

How can I deserialize this string to create an IInputStream (or IRandomAccessStream instead) which can be used in the StrokeContainer.LoadAsync() method?



Solution 1:[1]

You can use the following piece of code....

byte[] bytes = Convert.FromBase64String(stringinput);
MemoryStream stream = new MemoryStream(bytes);
IInputStream is=stream.AsRandomAccessStream(); //It will return an IInputStream object

Solution 2:[2]

Try this:

byte[] data = Convert.FromBase64String(encodedString);
InMemoryRandomAccessStream inputStream = new InMemoryRandomAccessStream();
await inputStream.WriteAsync(data.AsBuffer());
inputStream.Seek(0);
await canvas.InkPresenter.StrokeContainer.LoadAsync(inputStream);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rob Sedgwick
Solution 2