2017-04-13 121 views
0

NotSupportedException异常:无资料发现这个像素格式。WPF抑制NotSupportedException异常:“像素格式不支持”

我怎样才能抑制这种异常 ,我试着在try {}赶上(NotSupportedException异常),但它没有抓到应用程序进入中断模式每次。

我已经阅读了一些问题,如何处理它(当一个对磁盘映像文件),但我的问题是,我使用的taglib动态等有一个文件在磁盘上的图像生成这些图像。 (); this.loadedImage.EndInit

  if (f.Tag.Pictures.Length > 0) 
      { 
       TagLib.IPicture pic = f.Tag.Pictures[0]; 
       MemoryStream ms = new MemoryStream(pic.Data.Data); 
       ms.Seek(0, SeekOrigin.Begin);     

       this.loadedImage.BeginInit(); 
       this.loadedImage.CacheOption = BitmapCacheOption.OnLoad; 
       this.loadedImage.DownloadCompleted += this.OnDownloadCompleted; 
       this.loadedImage.DownloadFailed += this.OnDownloadFailed; 
       this.loadedImage.StreamSource = ms; 
       this.loadedImage.EndInit(); 
      } 
      else 
      { 
       // this.loadedImage = null; 
      } 
     } 
     catch (NotSupportedException el) 
     { 

      // MessageBox.Show(el.Message); 
     } 

`

+0

你尝试在操控性上应用了'DispatcherUnhandledException'事件? –

回答

1

除了它是一般的不良行为,以抑制不正确的异常处理(至少记录它例如),您需要更改的Visual Studio例外设置(我asssume你使用VS )你可以告诉它停止违反特定的例外。

Visual Studio的菜单调试 - >例外 - >取消选中列时抛出特定异常。

编辑: 在新的VS版本,你可以在这里找到这个: Visual Studio菜单调试 - > Windows - >异常设置 - >取消选中投掷列中的特定异常。

编辑2: 您也可以尝试取消选中该复选框,当VS异常的突破: You can also try unchecking that box when VS breaks on the exception

无论哪种方式,您的应用程序应该捕获异常,而不当您从Visual Studio中运行它不会崩溃。

+0

我编辑我的问题,检查错误是在这条线this.loadedImage.EndInit()发生异常箱后; –

0

从这个链接"No imaging component suitable to complete this operation was found."

有此评论

.png格式具有零文件大小的文件将会给这个相同的错误。

所以实际上,在将内存流数据传递给BitmapImage StreamSource之前,应该检查byte []数组,确保数据确实有效或字节数组长度大于零。

E.g

MemoryStream ms = new MemoryStream(pic.Data.Data); 
    if (pic.Data.Data.Length > 0) 
this.loadedImage.EndInit(); 

这将跳过带有因此零长度suppresing异常图像。

相关问题