【问题标题】:UWP app add an image (path) to SQLite DatabaseUWP 应用将图像(路径)添加到 SQLite 数据库
【发布时间】:2015-12-08 18:16:35
【问题描述】:

您好,我正在创建一个 UWP 应用,并且有以下方法来上传图片。

   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 应用程序进行编码。感谢您的帮助。

【问题讨论】:

    标签: image sqlite uwp


    【解决方案1】:

    我建议您将 byte[] 存储在 SQLite 表的 blob 列中。 这里有一篇关于它的好帖子http://andywigleyblog.azurewebsites.net/?p=117

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

    最重要的部分是转换方法

    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;
    }
    

    【讨论】:

      【解决方案2】:

      与 SQLite 的通信可以使用 Entity Framework 7,以及如何使用它的文章,可以找到here。但是有一个问题,它只是发布候选版本(EF 7.0.0-rc1)并且在编译到 .NET Native 时存在问题。

      【讨论】:

        猜你喜欢
        • 2020-03-25
        • 1970-01-01
        • 2012-01-18
        • 1970-01-01
        • 2013-02-22
        • 1970-01-01
        • 2021-02-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多