views:

335

answers:

3

Hi all

I'm trying to use the new WriteableBitmap in the Silverlight3 RTM, but I'm failing .. all the examples and demo I've used to look at and played with during the beta are no more working. I understood they've changed slightly the class interface, removing for example the Lock and Release methods (that are still documented in the official doc pages) and also changing the constructor (no more pixelformat as an argument, all the bitmap will be 32bit from my understanding).

Anyone managed to have an example working? Here there is a minimalistic example (I've found it in some forum and slightly modified it); it does not work, there is no bitmap displayed

Yes, I'm calling it .. here there is a minimal example (I've found around on the net and slightly modified it); it does not work, I got a blank page (the xaml contains a single Image control named inputImage).

    int imageWidth = 100;
    int imageHeight = 100;

    //Create the bitmap
    WriteableBitmap b = new WriteableBitmap(imageWidth, imageHeight);
    for (int x = 0; x < imageWidth; x++)
    {
        for (int y = 0; y < imageHeight; y++)
        {
            // generate a color in 32bit format
            byte[] components = new byte[4];
            components[0] = (byte)(x % 255);        // blue
            components[1] = (byte)(y % 255);        // green
            components[2] = (byte)(x * y % 255);    // red
            components[3] = 0;      // unused

            int pixelValue = BitConverter.ToInt32(components, 0);

            // Set the value for the 
            b.Pixels[y * imageWidth + x] = pixelValue;
        }
    }

    b.Invalidate();
    inputImage.Source = b;

Thanks for the help Riccardo

+1  A: 

Did you call the Invalidate method? That's what draws the image.

Peter Bromberg
Hi, yes, I'm calling Invalidate .. I've edited my original post with a simple demo I'm trying to run.
Orporick
A: 

I think you should also set the Width and Height of your inputImage in XAML to 100 x 100 in order to see the image...

Roboblob
+2  A: 

Hi there,

Change

components[3] = 0;

to

components[3] = 255;

and you get your picture.

This value represents the alpha channel value of a bitmap, since the only format WriteableBitmap supports is Pbgra32 (See http://www.cnblogs.com/shinyzhu/archive/2009/07/11/silverlight-3-released.html)

If you set it to 0 you get a white picture.

Rocky