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();

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:

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

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!!!!