Catastrophic failure with Bitmap.setSource

2. November 2011 10:06 by Mrojas in General  //  Tags: , , , , ,   //   Comments (0)

 

My friend Jesus has been working on an interesting task. We has been implementing some code so we can connect
a WIA compatible scanner with a Silverlight App.

Once he got the scanner to work, he had to send that data to the Silverlight app.
Something like:

var stream = new MemoryStream(data);
BitmapImage bmp = new BitmapImage();
bmp.SetSource(stream);

During this interesting test the infamous Catastrophic failure was raised. This exception
is caused if the data is in an invalid or unsupported format. You can see about this issue in
these links:

http://connect.microsoft.com/VisualStudio/feedback/details/436047/silverlight-3-bitmapimage-setsource-catastrophic-failure

http://forums.silverlight.net/p/232426/569554.aspx

However, Jesus found a interesting solution. Use an appropriate encoder. He found this page:

http://blogs.msdn.com/b/jstegman/archive/2009/09/08/silverlight-3-sample-updates.aspx

With references of Encoder for: .ICO, BMP and GIF

With this code you can do something beautiful like this:

Stream    file = …

// Decode
Texture texture = Silverlight.Samples.BMPDecoder.Decode(file); 
//Texture is another class in this sample that will return a writeable Bitmap //And with that texture object you just load the image like this: WriteableBitmap wb = texture.GetWriteableBitmap(); wb.Invalidate(); img.Source = wb; Nice!!!!

Categories