我有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 []中检索后图像的外观:
如果有人遇到这个问题并且有解决方案,将不胜感激。