Problem with Silverlight 3 WriteableBitmap

I try to use the new WriteableBitmap in RTM Silverlight3, but I fail. all the examples and demos that I used to view and play during beta testing no longer work. I realized that they slightly changed the class interface, removing, for example, locking and release methods (which are still documented on the official pages of the document), as well as changing the constructor (no more than pixelformat as an argument, all bitmaps will be 32 bits from my understanding )

Has anyone managed to work with an example? There is a minimalist example here (I found it on some forum and changed it a bit); it does not work, the bitmap is not displayed

Yes, I call it .. there is a minimal example here (I found it on the net and changed it a bit); it does not work, I got a blank page (xaml contains one 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 of Riccardo

+3
source share
2 answers

Edit

components[3] = 0;

to

components[3] = 255;

and you will get your photo.

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

If you set it to 0, you will get a white image.

+2
source

, XAML 100 x 100, ...

0

Source: https://habr.com/ru/post/1712542/


All Articles