Pregunta

I am new to Windows 8 development and Microsoft's technologies. I've done a lot of iOS development, but I've never touched Visual Studio, C#, etc before, so I am learning a lot (frameworks, IDEs, language) all at once. Forgive me if this is something simple, but I can't find an answer anywhere.

Note: I'm using Visual Studio Express 2013, C#, and Windows 8.1

For learning purposes I'm just trying to create an app that loads images, displays them on the screen, and allows the user to rotate, move, resize, and arrange them. I've got this working, except for one major bug that I can't figure out: When I load images from some directories everything works fine, when I load images from other directories I get E_NETWORK_ERROR and BitmapImage.ImageFailed. I can see no reason for this. I can actually take an image and put it in a directory and it will load, copy the same image to another directory, and it wont load.

Here's the code:

private async void addImageButton_Click(object sender, RoutedEventArgs e)
{
    Windows.Storage.Pickers.FileOpenPicker filePicker = new Windows.Storage.Pickers.FileOpenPicker();
    filePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
    filePicker.FileTypeFilter.Add(".jpg");
    filePicker.FileTypeFilter.Add(".png");
    filePicker.FileTypeFilter.Add(".bmp");
    filePicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
    Windows.Storage.StorageFile imageFile = await filePicker.PickSingleFileAsync();

    if (imageFile != null)
    {
        System.Diagnostics.Debug.WriteLine(imageFile.Path);
        Windows.UI.Xaml.Media.Imaging.BitmapImage bitmap = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri(imageFile.Path));
        System.Diagnostics.Debug.WriteLine(bitmap.UriSource);
        bitmap.CreateOptions = Windows.UI.Xaml.Media.Imaging.BitmapCreateOptions.None;
        Image img = new Image();
        img.Source = bitmap;
        this.theCanvas.Children.Add(img);
    }
}

This seems to work great with image files from some directories, and doesn't work at all with others. For example: in my Pictures library I have a "Camera Roll" directory that the Windows 8 camera app saves to, and I also have my Steam screenshots directory. If I try to open images in my Steam screenshots directory it works fine; however, if I try to load images in the Camera Roll directory it fails with E_NETWORK_ERROR. If I copy images from the Camera Roll directory to the Steam Screenshots directory I can open them.

Can anyone point out what's wrong? I'm starting to think this isn't a code problem so much as a security / capabilities / declarations type thing. Is there something I need to declare or request to get unfettered access to the filesystem? Do I need to form my URI differently or something?

EDIT: Here is some output from the debug statements showing the paths and URIs:

Loaded fine:

C:\Program Files (x86)\Steam\userdata\24321739\760\remote\72850\screenshots\2013-12-04_00001.jpg
file:///C:/Program Files (x86)/Steam/userdata/24321739/760/remote/72850/screenshots/2013-12-04_00001.jpg

Resulted in E_NETWORK_ERROR

C:\Users\Adam\Pictures\Camera Roll\WIN_20131023_194718.JPG
file:///C:/Users/Adam/Pictures/Camera Roll/WIN_20131023_194718.JPG
¿Fue útil?

Solución 2

I fixed this, but I'm not 100% sure I understand how/why the solution I arrived at resolved my problem.

I fixed the problem by switching to using a Windows.Storage.Streams.IRandomAccessStream to read the file into the BitmapImage Source:

private async void addImageButton_Click(object sender, RoutedEventArgs e)

{

Windows.Storage.Pickers.FileOpenPicker filePicker = new Windows.Storage.Pickers.FileOpenPicker();

filePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;

filePicker.FileTypeFilter.Add(".jpg");
filePicker.FileTypeFilter.Add(".png");
filePicker.FileTypeFilter.Add(".bmp");

filePicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;

Windows.Storage.StorageFile imageFile = await filePicker.PickSingleFileAsync();

if (imageFile != null)

{


Windows.UI.Xaml.Media.Imaging.BitmapImage bitmap = new Windows.UI.Xaml.Media.Imaging.BitmapImage();

Windows.Storage.Streams.IRandomAccessStream stream = await imageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);

Image newImage = new Image();

bitmap.SetSource(stream);

newImage.Source = bitmap;

newImage.Height = 250;
newImage.Stretch = Stretch.UniformToFill;
newImage.ManipulationMode = ManipulationModes.All;
newImage.ManipulationDelta += TestImage_ManipulationDelta;

this.theCanvas.Children.Add(newImage);

}

}

This resolved the issue and I can now load image files from any arbitrary location. However, I'm not 100% why this resolved the problem. I think it has something to do with async. I think that the way I was doing it before would somehow result in the BitmapImage or Image not containing the actual image data at display time. What I think this solution did was make the display code wait until we'd actually taken time to load the image. Even though this is solved, if a

Otros consejos

Weird issue, but I think that you should modify your code a bit as this answer here suggests. Instead of using the file path, try to create an image via loading a stream.

Just to be sure, does your app have the permission for accessing files in the user libraries?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top