2015-12-08 76 views
1

您好我正在創建一個UWP應用程序,並有以下方法來上傳圖片。UWP應用程序添加圖像(路徑)SQLite數據庫

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應用程序實現代碼。謝謝你的幫助。

回答

0

我建議你將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; 
} 
0

對於SQLite的通信可用於實體框架7,和文章,如何使用它,可以發現here。但是有問題,它只是發佈候選版本(EF 7.0.0-rc1),並且在編譯到.NET Native時出現問題。