InvalidOperationException with MediaLibrary pictures


I have a windows phone app and get sometimes InvalidOperationExceptions but not sure why and how to avoid them. The problem function from the error report is Microsoft.Xna.Framework.Media.MediaLibraryEnumerator_1[[System.__Canon,_mscorlib]].get_Item and i get this stacktrace

"Frame    Image                             Function                                                                                                                                    Offset        
0    Microsoft.Xna.Framework.Media.MediaLibraryEnumerator_1[[System.__Canon,_mscorlib]].get_Item                                                 0x0003e4d8    
1    Microsoft.Xna.Framework.Media.MediaLibraryEnumerator_1[[System.__Canon,_mscorlib]].System.Collections.IEnumerator.get_Current               0x00000006    
2    Microsoft.Xna.Framework.Media.MediaLibraryEnumerator_1[[System.__Canon,_mscorlib]].System.Collections.Generic.IEnumerator_T_.get_Current    0x0000001c    
3                   MapLense.Helper.PictureMapping.Add                                                                                                          0x000000a8    
4                   MapLense.Helper.PictureMapping+_GetPicture_d__b.MoveNext                                                                                    0x000000f6    
5                   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess                                                                              0x00216c46    
6                   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification                                                         0x0000003a    
7                   System.Runtime.CompilerServices.TaskAwaiter_1[[System.__Canon,_mscorlib]].GetResult                                                         0x0000001c    
8                   MapLense.Helper.Map+_AddPictureToMap_d__17.MoveNext                                                                                         0x00000118    
9                   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess                                                                              0x00216c46    
10                   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification                                                         0x0000003a    
11                   System.Runtime.CompilerServices.TaskAwaiter_1[[System.__Canon,_mscorlib]].GetResult                                                         0x0000001c    
12                   MapLense.MainPage+_ViewModelOnPropertyChanged_d__1e.MoveNext                                                                                0x00000204    
13                   System.Runtime.CompilerServices.AsyncMethodBuilderCore._ThrowAsync_b__0                                                                     0x00000036"

I also tried to add a try-catch block around the code block, but without a result

public static bool Add(DBPicture dbpicture)
    if (Pictures.ContainsKey(dbpicture.UniqueID))
        return true;

    var root = new MediaLibrary().RootPictureAlbum;

    foreach (var album in root.Albums)
        if (album.Name != AppResources.CameraRollAlbumName) continue;

        for (var i = 0; i < album.Pictures.Count; i++)
                var picture = album.Pictures[i];
                if (picture.Name == dbpicture.UniqueID)
                    Pictures.Add(picture.Name, picture);
                    DBPictures.Add(picture.Name, dbpicture);
                    return true;
            catch (System.Exception e)

    return false;

Thanks for any suggestions

asked on Stack Overflow Apr 29, 2013 by Philipp

3 Answers


Not really one answer but if the error is one the first foreach and the try is on the second it's not catching the exception put the try outside the

    var root = new MediaLibrary().RootPictureAlbum;
answered on Stack Overflow May 8, 2013 by Pedro.The.Kid • edited May 8, 2013 by Pedro.The.Kid

I had the same issue. Same crash for a small number of users. It's a very interesting problem and I believe it is with WP8 and not with your code. Finally I managed to repro it on a device but only occures if the debugger is detached. Two things you need to make sure.

  1. is not null
  2. Instead if enumerating the album.Pictures try something like album.Pictures.OrderBy(x=>x.Date);

I know it looks strange but this workaround works.

answered on Stack Overflow Nov 14, 2013 by Andras Csehi

I came across this exception today in my WP 8.0 app. The user gets several thumbnail images of his "Camera Roll" media library in a custom control. When he selects one of these thumbnail i try to get the real image via "GetImage()". The thumbnails worked fine, but GetImage() threw the exception. Apparently (that's my guess) some of the picture were corrupt or damaged. It never worked with them but other pictures had no problems.

So keep in mind that it might also be a damaged or corrupt file.

answered on Stack Overflow Oct 9, 2015 by Khrimm

User contributions licensed under CC BY-SA 3.0