2012-06-07 69 views
0

我指的是这个http://www.dotnetcurry.com/ShowArticle.aspx?ID=585从sql server检索到窗口电话的图像,并有错误ArgumentNullException时,它运行在那个高亮行。错误ArgumentNullException未处理

public class ImageConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      byte[] buffer = value as byte[]; 
      **Stream memStream = new MemoryStream(buffer);** 
      WriteableBitmap wbimg = PictureDecoder.DecodeJpeg(memStream); 
      return wbimg; 
     } 
     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      return null; 
     } 
    } 

我发现了另一个类似的帖子和我有同样的错误。 http://forums.create.msdn.com/forums/p/74224/452127.aspx 但不明白编码..任何人都知道如何解决错误?

+0

这正是例外情况所说的。 'buffer'为空。 –

回答

0

看起来像为空或不是字节数组。

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
{ 
    WriteableBitmap wbimg = null; 

    if (value != null && value is byte[]) { 
     byte[] buffer = value as byte[]; 
     Stream memStream = new MemoryStream(buffer); 
     wbimg = PictureDecoder.DecodeJpeg(memStream); 
    } 

    return wbimg; 
} 
+0

但是,当我在我的编码中添加 if(value!= null && value是byte []) 。它告诉我错误不是所有代码路径都返回一个值。 –

+0

我编辑了答案以显示完整的源代码。 – igoris

+0

我已经试过了。值为空......我认为可能是因为概率是在转换图像的部分。我使用[http://www.redmondpie.com/inserting-in-and-retrieving-image-from-sql-server-database-using-c/]将图像插入到sql server中。你能帮我看看编码吗?我仍然试图理解这两种编码。 –

相关问题