将字符串转换为流

时间:2017-07-27 12:54:43

标签: c# .net uwp windows-10-universal

所以我尝试为我的UWP应用程序从IReadOnlyList<InkStroke>序列化和反序列化Windows.UI.Input.Inking,我使用以下代码来序列化数据:

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);

如何反序列化此字符串以创建可在IInputStream方法中使用的IRandomAccessStream(或StrokeContainer.LoadAsync())?

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码安静....

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

答案 1 :(得分:1)

尝试this

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