BitmapSource: WPF Bitmaps
Friday, 04 December 2009
Article Index
BitmapSource: WPF Bitmaps
Paletted bitmaps

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:

 int width=10;
int height=20;
int stride=width +(width) %4;
byte[] bits=new byte[height*stride];

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,
int x, int y,int stride,byte c)
bits[x + y * stride] = c;

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 );
setpixel(ref bits, 2, 19, stride, 7);
setpixel(ref bits, 3, 19, stride, 9);

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(
width, height ,
300, 300,
bits, stride);

image1.Source = bs;

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.

Modifying BitmapSource

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];
bs.CopyPixels(bits2, stride, 0);

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(
width, height,
300, 300,

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.


Custom Bitmap Effects - Getting started

The simplest possible custom effects project is enough for you to see how it all works and to move on to building your own effects that do something useful. This is an introduction to using HLSL in WP [ ... ]

WPF .NET Core - Creating Objects With XAML

If you've never encountered WPF (Windows Presentation Foundation) you are missing a versatile tool. This article is part of a series devoted to it. XAML can be confusing - especially if you think it i [ ... ]

Custom Bitmap Effects - HLSL

In the article Custom Bitmap Effects - Getting started we discovered how to work with HLSL in WPF. Now we are in a position to write more sophisticated shaders and  this means learning some more  [ ... ]

Getting Started with WPF


WPF Windows Presentation Foundation is the new way to do forms and all things graphical in .NET. You can still use  Windows Forms but don't expect anything particularly new to be added to [ ... ]

Drawing Bitmaps – DrawingImage and DrawingVisual


WPF provides multiple ways to convert vector drawings to bitmaps. Find out how DrawingImage and DrawingVisual work and when to use which. On the way we look at how to create 2D vector drawings.

Other Articles



Last Updated ( Monday, 01 August 2011 )