正在从SQLite c#Windows应用程序加载图像
本文关键字:应用程序 加载 图像 c#Windows SQLite | 更新日期: 2023-09-27 18:27:34
我得到错误:"App2.exe中发生了类型为"System.ArgumentException"的异常,但未在用户代码中处理,其他信息:值不在预期范围内"。
这是在尝试将图像加载到我的应用程序时发生的。我有下面的代码从我的数据库加载图像。filename变量返回:"Y:''''Pictures''''Cake.jpg",这是我试图加载的图像路径。感谢您的帮助。
string FilePath = @"Y:'Pictures'";
FileName = FilePath + ms.RecipeImage;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(FileName, UriKind.Absolute));
IRandomAccessStream filestream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
BitmapImage bmpImage = new BitmapImage();
bmpImage.SetSource(filestream);
image.Source = bmpImage;
保存到数据库:
public 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;
}
}
}
根据本页:https://msdn.microsoft.com/de-de/library/windows/apps/windows.storage.storagefile.getfilefromapplicationuriasync您只能将GetFileFromApplicationUriAsync与这些URI格式"ms-appx://"或"ms-app/"一起使用。例如,您应该能够使用此方法从assets文件夹加载资源。
编辑:无法访问所有文件和文件夹(https://msdn.microsoft.com/en-us/library/windows/apps/mt188700.aspx)。但在您的情况下,这应该可以工作(如果您要加载的文件位于我的图片文件夹中)。
var filestream = await Windows.Storage.KnownFolders.PicturesLibrary.OpenStreamForReadAsync("Screenshots''Screenshot (1).png");
InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
await filestream.CopyToAsync(ras.AsStreamForWrite());
BitmapImage bmpImage = new BitmapImage();
bmpImage.SetSource(ras);
image.Source = bmpImage;
不要忘记选中软件包中的图片库复选框。appxmanifest.