问题
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;
}
}
}
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.
回答1:
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;
}
回答2:
For communication with SQLite could be used Entity Framework 7, and article, how to use it, could be found here. But there is on problem, it just release candidate (EF 7.0.0-rc1) and has problems with compilation to .NET Native.
来源:https://stackoverflow.com/questions/34146359/uwp-app-add-an-image-path-to-sqlite-database