gpt4 book ai didi

c# - 没有缩略图的文件在 Win 8 Metro 中抛出 COM 异常

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:08 26 4
gpt4 key购买 nike

在我的应用程序中,我需要从使用文件选取器选取的文件中抓取缩略图。当文件选取器返回一个没有缩略图的文件(例如空的 *.bmp 文件)时,以下行将引发 COM 异常。我该如何避免这种情况?

 StorageItemThumbnail t = await f.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.DocumentsView);

编辑:其他详细信息,我的意思是 .bmp 而不是 .png 抱歉...

重现:

  1. 在 Metro 桌面模式下右键单击任意目录,在上下文菜单中单击“新建”->“位图图像”
  2. 在测试程序中,启动文件选取器,然后对返回的文件调用 GetThumbnailAsync;你得到下面的异常(exception)。

异常详情:

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

启动文件选择器的代码:

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;
}

最佳答案

正如上面的评论以及 MSDN 论坛上的某人所提到的。解决方法是在 try-catch block 中调用 GetThumbnailAsync 并在没有缩略图时放置占位符图像。

StorageItemThumbnail t=null;
BitmapImage thumbnailImage= new BitmapImage();//image used for display
try{
t = await f.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.DocumentsView);
}
catch(Exception e){
t = null;
}
if (null == t)
thumbnailImage = placeholder;//no thumbnail then use the placeholder image
else
thumbnailImage.SetSource(t);

关于c# - 没有缩略图的文件在 Win 8 Metro 中抛出 COM 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9525466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com