|BitmapSource: WPF Bitmaps|
|Friday, 04 December 2009|
Page 2 of 2
A paletted bitmap
Most other BitmapSource tasks vary little from the above example. The one exception is perhaps creating an indexed or paletted bitmap. A palette is simply an array of colours and each pixel simply stores an index into the array. For example, the most common paletted bitmap uses an 8-bit index giving a palette of 256 colours. To create a paletted bitmap following the same steps for the RGB bitmap is fairly easy:
Notice that in this case there is one byte per pixel and hence the stride is just the row width rounded up to the nearest multiple of four. The equivalent of the setpixel function is:
private void setpixel(ref byte bits,
In this case the colour is determined by an index between 0 and 255 into the palette array. Once we have the setpixel function we can set a few pixels:
setpixel(ref bits, 0,0,stride,34 );
Notice that the colours that have been selected depend on what 34, 7 and 9 correspond to in the palette. Finally we create the BitmapSource, this time specifying an 8-bit palette and a particular palette of colours – in this case the WebPalette:
BitmapSource bs = BitmapSource.Create(
If you run this program you should see some coloured pixels displayed.
Of course to do anything realistic you need to know what colours are stored in the palette. The Colors property of the BitmapPalette object can be used to provide a list of colours in the palette.
Notice that you can’t modify the bitmap once the BitmapSource has been created. In this sense the BitmapSource is immutable just like a String.
For example, if you change the byte array it has no effect as the bits in the BitmapSource have been read from the array and fixed in memory.
However, this doesn’t mean that you can’t access and modify the pixels in a BitmapSource but it isn’t very efficient.
You can retrieve the pixels in a BitmapSource into a byte array:
byte bits2=new byte[height*stride];
The final parameter is an offset that determines where in the image the copy starts. There are also two versions of the CopyPixels method which allow you to specify the rectangle that contains the pixels that you want to copy and one that copies them to unmanaged memory. Once you have the pixels you can modify them just as you can modify any byte array:
setpixel(ref bits2, 4, 19, stride,
However, to get them back into a BitmapSource you have to recreate it as a new object:
bs = BitmapSource.Create(
and to see it you have to assign it again to the Image control’s source:
image1.Source = bs;
You might find this idea of destroying and creating new BitmapSource objects a terrible way to work, but as long as you don’t do it too often it’s a reasonable strategy.
It is certainly the way that more complex classes such as TransformedBitmap,CroppedBitmap and FormatConvertedBitmap, which are derived from BitmapSource, are used. In general you modify BitmapSources by creating new ones derived from existing ones.
The next thing we need to look at in detail is the BitmapImage class which inherits all of BitmapSource’s methods but can access bitmaps from a wider range of sources.
|Last Updated ( Monday, 01 August 2011 )|