Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to display an image from a SQL Server 2008 R2 database into an Image control in my Silverlight 4 desktop application. I have confirmed in the debugger that the record is retrieving correctly and the value in the varbinary(max) field has the binary data in it.

The problem is occurring when I call the SetSource method on the BitmapImage field and try to set it to the MemoryStream variable named ms. It generates a "Catastrophic failure" and I have no clue how to resolve this. Any ideas or suggestions would be greatly appreciated since I have now spent 2 days on trying to make this image display on the screen correctly.

CODE
if (photoDomainDataSource.DataView.Count > 0)
{
  Photo p = (Photo)photoDomainDataSource.DataView[0];
  MemoryStream ms = new MemoryStream(p.FileBlob, 0, p.FileBlob.Length);
  BitmapImage bmp = new BitmapImage();
  bmp.SetSource(ms);
  fileBlobImage.Source = bmp;
}



ERROR
{System.Exception: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource, CValue& byteStream)
   at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource)
   at System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource)
   at System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource)
   at Nomad.Views.PhotoControl.photoDomainDataSource_LoadedData(Object sender, LoadedDataEventArgs e)}
    Data: {System.Collections.ListDictionaryInternal}
    InnerException: null
    Message: "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))"
    StackTrace: "   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)\r\n   at MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource, CValue& byteStream)\r\n   at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource)\r\n   at System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource)\r\n   at System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource)\r\n   at Nomad.Views.PhotoControl.photoDomainDataSource_LoadedData(Object sender, LoadedDataEventArgs e)"
Posted
Comments
Sergey Alexandrovich Kryukov 16-Jun-11 12:09pm    
This is not a proper issue report. In what line of code it happens? Where is the exception dump (with Exception.Stack, inner exceptions)?
--SA
Ron Icard 16-Jun-11 12:57pm    
This is a cut and paste of the error from Visual Studio. As I said in the original post, the error occurs in the line:
bmp.SetSource(ms);

Apparently, setting the bitmap image to the memory stream is causing a memory error but I have no clue how to determine what it is or how to fix it.
Mark Salsbery 17-Jun-11 19:56pm    
What format is the image data? If you write the contents of p.FileBlob to a file, can you load the image from that file in other image viewing software like Paint?
sillett 19-Jun-11 21:15pm    
I am having the exact same issue in trying to convert a JPEG that is being received as a byte array from a web service. Once I call SetSource(), it crashes. I've tried JPEG and PNG formats. I also tried to eliminate the MemoryStream by writing the byte array to isolated storage and then using that stream to bring it back. No matter what Stream I feed to bmp.SetSource(), it fails.
Ron Icard 20-Jun-11 8:26am    
Mark, yes I can load the image if it's coming from a file and that doesn't crash. The image I'm getting is from a varbinary(max) field in SQL Server. When I try to call the bmp.SetSource method, it crashes with that "Catastrophic failure" message I listed.

I thought about putting in code to just dump every record to a temporary file and then read that file in to display it so that it doesn't give me the memory error. But, besides being really slow and cumbersome, that will make for some nasty disk cleanup issues that I don't wish to deal with.

I'm REALLY hoping someone else has seen this issue and figured out a work around. Otherwise, I'm dead in the water at the moment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900