Windows 8.1存储xaml将InkManager保存在一个字符串中

时间:2014-06-16 09:53:12

标签: image xaml windows-store-apps drawing windows-8.1

我正在尝试将我用铅笔绘制的内容保存为字符串,我通过SaveAsync()方法将其放入IOutputStream中,然后使用AsStreamForWrite()方法将此IOutputStream转换为流事情应该没问题,但是如果我使用例如这个代码块,那么我在这部分之后会遇到很多问题:

  using (var stream = new MemoryStream())
                {
                    byte[] buffer = new byte[2048]; // read in chunks of 2KB
                   int bytesRead = (int)size;
                    while (bytesRead < 0)
                    {
                        stream.Write(buffer, 0, bytesRead);

                    }
                    byte[] result = stream.ToArray();
                    // TODO: do something with the result
                }

我得到了这个例外

"Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection." 

或者如果我尝试使用InMemoryRandomAccessStream将流转换为图像:

 InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
                await s.CopyToAsync(ras.AsStreamForWrite());

我的InMemoryRandomAccessStream变量的大小始终为零。

也尝试了

StreamReader.ReadToEnd();

但它返回一个空字符串。

1 个答案:

答案 0 :(得分:0)

在这里找到答案:

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/2359f360-832e-4ce5-8315-7f351f2edf6e/stream-inkmanager-strokes-to-string

private async void ReadInk(string  base64)
{
if (!string.IsNullOrEmpty(base64))
{
    var bytes = Convert.FromBase64String(base64);

    using (var inMemoryRAS = new InMemoryRandomAccessStream())
    {
        await inMemoryRAS.WriteAsync(bytes.AsBuffer());
        await inMemoryRAS.FlushAsync();
        inMemoryRAS.Seek(0);

        await m_InkManager.LoadAsync(inMemoryRAS);

        if (m_InkManager.GetStrokes().Count > 0)
        {
            // You would do whatever you want with the strokes
            // RenderStrokes(); 
        }
    }
}
}