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

在windows应用商店应用程序的sqlite中存储和显示图像?,sqlite,windows-store-apps,Sqlite,Windows Store Apps,我有一个表单,我想从中将一个图像存储到我的sqlite数据库中,然后我希望它作为网格背景显示在另一个页面上。 我正在制作一个windows应用商店应用程序,所以我正在使用xaml和c。 我希望存储的图像作为我的gridview背景。您可以将其存储为base64编码图像,当您需要显示它时,必须对图像进行解码 尝试读取Base-64是在SQLite中存储图像的最佳编码技术。请尝试下面给定的代码。一种方法将为您提供存储文件的base-64编码字符串,另一种方法将返回可设置为源的位图图像对象 专用异步任

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

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


尝试读取Base-64是在SQLite中存储图像的最佳编码技术。请尝试下面给定的代码。一种方法将为您提供存储文件的base-64编码字符串,另一种方法将返回可设置为
源的
位图图像
对象

专用异步任务Base64StringToBitmap(字符串源)
{
var ims=新的InMemoryRandomAccessStream();
var bytes=Convert.FromBase64String(源);
var dataWriter=新的dataWriter(ims);
dataWriter.WriteBytes(字节);
等待dataWriter.StoreAsync();
ims.Seek(0);
var img=新的位图图像();
img.SetSource(ims);
返回img;
}
专用异步任务转换器StorageFileToBase64String(StorageFile imageFile)
{
var stream=await imageFile.OpenReadAsync();
使用(var dataReader=newdatareader(流))
{
var bytes=新字节[stream.Size];
等待dataReader.LoadAsync((uint)stream.Size);
dataReader.ReadBytes(字节);
返回Convert.tobase64字符串(字节);
}
} 

您可以通过两种方式将图像存储到数据库中-

  • 通过将图像转换为
    字节[]
  • 将图像转换为bytes[]并将其保存为sqlite Blob类型参数
  • 获取Blob数据类型并再次存储到字节[],然后转换为图像
  • 通过将图像转换为base64字符串
  • 将image-to-base64字符串存储转换为varchar/varchar2类型的sqlite
  • 从sqlite db获取base64字符串并转换为图像

  • 我可以得到这个问题的示例代码吗??我是一个初学者,所以我不知道在哪里写代码以及如何传递变量。我可以有这个问题的示例代码吗??我是一个初学者,所以我不知道在哪里写代码以及如何传递变量。?
    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);
        }
    }