将bitmapimage转换为字节[]数组以存储在sqlite数据库中

将bitmapimage转换为字节[]数组以存储在sqlite数据库中,sqlite,bytearray,bitmapimage,Sqlite,Bytearray,Bitmapimage,我使用filepicker从本地文件夹中选择一张照片,并将该图像分配给xaml页面中的图像控件。现在图像已经存在,我想将其保存到sqlite数据库中的byte[]字段中。我曾尝试过多种搜索互联网的方法,但大多数都涉及到一个名称空间,而这个名称空间与windows 8.1 windows应用商店应用程序中的System.Drawing不同。你能推荐一种方法吗?下面是我用来将byte[]数组转换为位图图像的方法。我只想要相反的结果: //this converts VehPhoto Byte[

我使用filepicker从本地文件夹中选择一张照片,并将该图像分配给xaml页面中的图像控件。现在图像已经存在,我想将其保存到sqlite数据库中的byte[]字段中。我曾尝试过多种搜索互联网的方法,但大多数都涉及到一个名称空间,而这个名称空间与windows 8.1 windows应用商店应用程序中的System.Drawing不同。你能推荐一种方法吗?下面是我用来将byte[]数组转换为位图图像的方法。我只想要相反的结果:

   //this converts VehPhoto Byte[] array to BitMapImage file
    private async Task LoadImageAsync()
    {
        using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
        {
            // Writes the image byte array in an InMemoryRandomAccessStream
            // that is needed to set the source of VehicleBitMap.
            using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
            {
                writer.WriteBytes(vehphoto);
                await writer.StoreAsync();
            }

            var image = new BitmapImage();
            await image.SetSourceAsync(ms);
            vehiclebitmap = image;
        }
    }

谢谢你的帮助

我终于找到了自己问题的答案。我突然意识到,只要从文件中加载照片图像,就可以将其放入字节数组。因此,在我使用filepicker选择照片之后,在将其转换为BitMapImage以用于图像控件之后,我使用以下代码将其分配给我的数据库元素:

            byte[] fileBytes = null;
            using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync())
            {
                fileBytes = new byte[stream.Size];
                using (DataReader reader = new DataReader(stream))
                {
                    await reader.LoadAsync((uint)stream.Size);
                    reader.ReadBytes(fileBytes);
                    Vehicle.VehPhoto = fileBytes;
                }
            }
这样就不需要将位图图像转换为字节[],只需要将文件转换为字节[]。是的