3

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()同じエラーなど、他の操作を試しました。この機能を正しく動作させるために必要な別の機能はありますか?

編集:

@LTの回答に基づいて、他の機能をいくつか試しました。以下は私に同じエラーを与えます:

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

ただし (明らかに、私が音楽機能を提供している場合)、これは次のことを行いません。

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

これが私の写真ライブラリに固有のものであることは間違いありませんが、それが何であるかはわかりません。

4

3 に答える 3

2

コストが画像をプレビューするだけの場合。これを使用できます

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

画像をキャンバスに追加します。ただし、最初に test.png をプロジェクト アセットに追加する必要があります。または、Uri をテスト イメージの場所に変更することもできます。

于 2014-05-28T06:23:04.877 に答える
0
   <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Image Name="img"></Image>
        <Button Name="btn" Click="Btn_OnClick"></Button>

    </Grid>


private async void Btn_OnClick(object sender, RoutedEventArgs e)
        {
            var file = await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync("images.jpg");
            var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
            var image = new BitmapImage();
            image.SetSource(fileStream);
            img.Source = image;

        }

私はwindows8.1とvs.2013を持っています。エラーは表示されません。他の何かでしょうか?

于 2014-06-03T04:21:23.580 に答える
0
    //////to load an image file just do this
//put this in your app.xaml
protected override void OnActivated(IActivatedEventArgs args)
    {
        var root = Window.Current.Content as Frame;
        var mainPage = root.Content as MainPage;
        if (mainPage != null && args is FileOpenPickerContinuationEventArgs)
        {
            mainPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs);
        }
    }

//and this in your btn event


private void Button_Click(object sender, RoutedEventArgs e)
{
    var openPicker = new FileOpenPicker
    {
        ViewMode = PickerViewMode.Thumbnail,
        SuggestedStartLocation = PickerLocationId.PicturesLibrary
    };
    openPicker.FileTypeFilter.Add(".jpg");
    openPicker.PickSingleFileAndContinue();
}

//and this in you page

public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs fileOpenPickerContinuationEventArgs)
{
    if (fileOpenPickerContinuationEventArgs.Files != null)
    {
        // Do something with selected file/s
    }
}
于 2014-11-15T19:39:44.567 に答える