我有一张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
项目的图像源。这是我用来将位图转换为字节 [] 的方法:
using (Stream stream = ((WriteableBitmap)image.Source).PixelBuffer.AsStream())
{
data = new byte[(uint)stream.Length];
await stream.ReadAsync(data, 0, data.Length);
}
而这个恢复位图形式的字节[]:
WriteableBitmap wb = new WriteableBitmap(200,200);
using (Stream stream = wb.PixelBuffer.AsStream())
{
await stream.WriteAsync(data, 0, data.Length);
}
现在这里的屏幕截图描述了从 InkCanvas 检索和从 byte[] 检索后图像的外观:
如果有人遇到这个问题并有解决方案,将不胜感激。