Dynamic Image Generation in Silverlight

 

I’ve posted a sample PNG image generator here (Beta 1 bits). 

 

This has 3 dynamically generated images – one randomly filled 64x64 image that’s updated at 6 frames/sec. A second 128x128 image with a gradient fill and a third that’s a 512x512 dynamically generated image of the Mandelbrot set. When running the page you’ll see performance is quite good (each page refresh will re-create all images). Note that there are still some optimizations that can be made to minimize re-writes of bytes and if anyone updates this, let me know and I’ll post the updated sample.

 

You can find the source here. It’s fairly small as I don’t generate a compressed image and therefore don’t need the PNG compression library (ZLIB). On the flip side, the dynamically generated image is not well suited for local storage since its not compressed.

 

The API to dynamically generate an image and assign it to an Image element is fairly straight-forward:

      ei = new EditableImage(height, width); // EditableImage class from this sample

      image = new BitmapImage();

      // Generate gradient filled image

      for (int idx = 0; idx < height; idx++) // Height (y)

      {

        for (int jdx = 0; jdx < width; jdx++) // Width (x)

        {

          ei.SetPixel(jdx, idx, (byte)idx, (byte)jdx, 255, 255);

        }

      }

      // Get stream and set image source

      image.SetSource(ei.GetStream());

      img2.Source = image; // img2 is an Image element

 

 

Image sample