c# - 在 WinRT 中加载图像文件

标签 c# windows-runtime comexception

我有一个 WinRT 项目,在尝试预览图像时遇到错误。我已设置允许访问图片库的功能,并且正在使用以下代码:

 var file = await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync(path);
 var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
 var img = new BitmapImage();
 img.SetSource(fileStream);

这个错误出现在第一行:

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

Additional information: Error HRESULT E_FAIL has been returned from a call to a COM component.

我试过其他操作,比如 folder.GetFilesAsync() 也有同样的错误。我是否需要其他功能或功能才能使此功能正常工作?

编辑:

根据@L.T.s 的回答,我尝试了一些其他的功能。以下给了我同样的错误:

var folder = KnownFolders.PicturesLibrary;            
var files = await folder.GetFilesAsync();

但是(显然,如果我提供音乐功能)这不会:

var testfolder = KnownFolders.MusicLibrary;
var files = await testfolder.GetFilesAsync();

我不怀疑这是我的图片库特有的东西,但我不知道那可能是什么。

最佳答案

如果您的成本只是预览图像。你可以用这个

        Uri uri = new Uri("ms-appx:///Assets/test.png");
        BitmapImage bitmap = new BitmapImage(uri);
        Image image = new Image();
        image.Source = bitmap;

并将图像添加到 Canvas 。但是您需要先将 test.png 添加到您的项目 Assets 中,或者您可以将 Uri 更改为测试图像的位置。

关于c# - 在 WinRT 中加载图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23776336/

相关文章:

c# - 如何在 C# 中获取 Excel.Application 实例?

c# - C# 中的 jscript document.getElementsByName

c# - JSON : what is the java Map equivalent in c#

C# 如何使 foreach 循环不覆盖字符串变量

c# - WinRT 创建一个文件(如果该文件不存在)并知道该文件已创建

c# - 在桌面上拖动文件时可能导致 COMExceptions 的原因是什么?

c# - 使用 HttpClient 进行异步文件下载

c# - 温特市的 Bing map

windows-runtime - 我应该将 Metro 应用程序的 TemporaryKey.pfx 文件添加到版本控制中吗?

ASP.NET 4.0 Web 应用程序抛出 "Incorrect function. (Exception from HRESULT: 0x80070001)"