UWP - 从字节数组加载WritableBitmap返回奇怪的图像

时间:2017-02-19 14:01:18

标签: c# arrays uwp inkcanvas writablebitmap

我有InkCanvas绘图,我将其转换为WriteableBitmap并设置为ImageSource,如下所示:

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;

工作正常。

现在我需要将WriteableBitmap作为字节数组存储在数据库中,以便将其用作GridView项的图像源。这是我用来将位图转换为byte []的方法:

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

这个用于恢复位图格式byte []:

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

现在,这里的屏幕截图描述了从InkCanvas检索后以及从byte []中检索后图像的外观:

enter image description here

如果有人遇到这个问题并且有解决方案,将不胜感激。

0 个答案:

没有答案