In my application, I need to grab the thumbnail image from files picked using File Picker. The following line throws COM exception when File Picker returns a file that does not have a thumbnail image such as an empty *.bmp file. How do I avoid this?
StorageItemThumbnail t = await f.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.DocumentsView);
EDIT: additional details and I meant .bmp not .png sorry…
To reproduce:
- Right click in any directory in Metro desktop mode, in the context menu click New->Bitmap Image
- In a test program, launch File Picker, then call GetThumbnailAsync on that file returned; you get the exception below.
Exception Details:
System.Runtime.InteropServices.COMException was unhandled by user code
HResult=-2147467259
Message=Error HRESULT E_FAIL has been returned
from a call to a COM component.
Source=mscorlib
ErrorCode=-2147467259 StackTrace:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
—some stuff ommitted—
InnerException: null
Code to launch the File Picker:
public static async Task<IReadOnlyList<StorageFile>> PickMulipleFilesAsync()
{
FileOpenPicker picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.Desktop;
picker.FileTypeFilter.Add("*");
var files = await picker.PickMultipleFilesAsync();
return files;
}
As mentioned in above comments and also by someone on MSDN forums. A workaround is simply the call to GetThumbnailAsync in a in a try-catch block and put a placeholder image when there is no thumbnail.