添加到SQLite数据库

添加到SQLite数据库

本文介绍了UWP应用将图像(路径)添加到SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个UWP应用,并具有以下上传图片的方法.

Hi I am creating a UWP app and have the following method to upload a picture.

   async void imageButton_Click(object sender, RoutedEventArgs e)
    {
        FileOpenPicker open = new FileOpenPicker();
        open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        open.ViewMode = PickerViewMode.Thumbnail;

        // Filter to include a sample subset of file types
        open.FileTypeFilter.Clear();
        open.FileTypeFilter.Add(".bmp");
        open.FileTypeFilter.Add(".png");
        open.FileTypeFilter.Add(".jpeg");
        open.FileTypeFilter.Add(".jpg");

        // Open a stream for the selected file
        StorageFile file = await open.PickSingleFileAsync();

        // Ensure a file was selected
        if (file != null)
        {
            // Ensure the stream is disposed once the image is loaded
            using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                // Set the image source to the selected bitmap
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.DecodePixelHeight = 200;
                bitmapImage.DecodePixelWidth = 200;

                await bitmapImage.SetSourceAsync(fileStream);
                image.Source = bitmapImage;
            }
        }
    }

我有一个用SQLite创建的数据库,我想将图像路径添加到数据库中,但是我不知道如何使用UWP应用程序对此进行编码.感谢您的帮助.

I have a database created in SQLite and I would like to add the image path to the database but I have no idea how to code this with a UWP app. Thanks for any help.

推荐答案

我建议您将byte []存储在SQLite表的blob列中. http://andywigleyblog.azurewebsites.net/?p=117

I would suggest you store the byte[] in a blob column of a SQLite table.There is a good post about it available here http://andywigleyblog.azurewebsites.net/?p=117

它具有读取和保存图像所需的信息.

It has the needed info to read and save the image.

最重要的部分是转换方法

Most import part is the conversion method

private byte[] ConvertToBytes(BitmapImage bitmapImage)
{
    byte[] data = null;
    using (MemoryStream stream = new MemoryStream())
    {
        WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage);
        wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
        stream.Seek(0, SeekOrigin.Begin);
        data = stream.GetBuffer();
    }

    return data;
}

这篇关于UWP应用将图像(路径)添加到SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:58