在Windows应用商店的sqlite中存储和显示图像?

时间:2013-09-26 18:24:08

标签: sqlite windows-store-apps

我有一个表单,我想将图像存储到我的sqlite数据库,然后我希望它在另一个页面显示为网格背景。 我正在制作一个Windows应用商店应用,所以我使用的是xaml和c#。 我想将存储的图像作为我的gridview背景。

3 个答案:

答案 0 :(得分:1)

您可以将其存储为base64编码图像,当您需要显示它时,您必须解码图像。

尝试阅读this

答案 1 :(得分:1)

Base-64是在SQLite中存储图像的最佳编码技术。试试下面给出的代码。一种方法将为您提供基数为64的StorageFile&amp;编码字符串。另一个会返回BitmapImage个对象,可以将其设置为<Image />的来源。

private async Task<BitmapImage> Base64StringToBitmap(string source)
{
    var ims = new InMemoryRandomAccessStream();
    var bytes = Convert.FromBase64String(source);
    var dataWriter = new DataWriter(ims);
    dataWriter.WriteBytes(bytes);
    await dataWriter.StoreAsync();
    ims.Seek(0);
    var img = new BitmapImage();
    img.SetSource(ims);
    return img;
}

private async Task<string> ConvertStorageFileToBase64String(StorageFile imageFile)
{
    var stream = await imageFile.OpenReadAsync();

    using (var dataReader = new DataReader(stream))
    {
        var bytes = new byte[stream.Size];
        await dataReader.LoadAsync((uint)stream.Size);
        dataReader.ReadBytes(bytes);

        return Convert.ToBase64String(bytes);
    }
} 

答案 2 :(得分:1)

您可以使用两种方式将图像存储到数据库 -

  1. 将图片转换为byte[]
    1. 将Image转换为bytes []并将其保存为sqlite Blob类型参数。
    2. 获取Blob数据类型并再次存储到byte []然后转换为Image
  2. 通过将图像转换为base64字符串
    1. 将图像转换为base64字符串存储为varchar / varchar2类型的
    2. 从sqlite db获取base64字符串并转换为图像