我正在尝试将我用铅笔绘制的内容保存为字符串,我通过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();
但它返回一个空字符串。
答案 0 :(得分:0)
在这里找到答案:
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();
}
}
}
}