views:

333

answers:

2

A few months ago I built some online samples like this one from Jeff Prosise that use the WriteableBitmap class in Silverlight.

Revisiting them today with the latest Silverlight3 installer (3.0.40624.0), the API seems to have changed.

I figured out some of the changes. For example, the WriteableBitmap array accessor has disappeared, but I found it in the new Pixels property, so instead of writing:

 bmp[x]

I can write

bmp.Pixels[x]

Are there similar simple replacements for these calls, or has the use pattern itself changed?

bmp = new WriteableBitmap(width, height, PixelFormats.Bgr32);
bmp.Lock();
bmp.Unlock();

Can anybody point me to a working example using the updated API?

A: 

What happens if you don't use Lock and Unlock and just use the WritabelBitmap(int, int) constructor? Do things break?

It would seem that between SL3 Beta and the release this API has changed. See Breaking Changes Document Errata (Silverlight 3)

AnthonyWJones
Thanks, that changes document clarifies things. (Alas I do then just get a blank screen, but maybe that's because of the Bgr32/Pbgra32 difference, or something else entirely.)
Eric
+1  A: 

Another important detail about switching to the new WriteableBitmap is given in this answer ... because the pixel format is now always pbgra32, you must set an alpha value for each pixel, otherwise you just get an all-white picture. In other words, code that formerly generated pixel values like this:

byte[] components = new byte[4];
components[0] = (byte)(blue % 256);       // blue
components[1] = (byte)(grn % 256);        // green
components[2] = (byte)(red % 256);        // red
components[3] = 0;                        // unused

should be changed to read:

byte[] components = new byte[4];
components[0] = (byte)(blue % 256);       // blue
components[1] = (byte)(grn % 256);        // green
components[2] = (byte)(red % 256);        // red
components[3] = 255;                      // alpha
Eric