BitmapSource: WPF Bitmaps
Written by Administrator   
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,
PixelFormats.Indexed8,
BitmapPalettes.WebPalette,
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,
 Colors.Red);

However, to get them back into a BitmapSource you have to recreate it as a new object:

 bs = BitmapSource.Create(
width, height,
300, 300,
PixelFormats.Rgb24,
null,
bits2,
stride);

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.

Banner


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 [ ... ]



The bitmap transform classes

WPF bitmaps are immutable but that doesn't mean you can't transform them. Find out about rotation, scaling, changing pixel formatting and how to mangage color profiles.



WPF .NET Core - Inside Dependency Properties

One of the great mysteries of WPF is the strangely named "Dependency Properties". In this chapter we learn how dependency properties really work by creating a custom dependency property



WPF The Easy 3D Way

WPF provides a full 3D graphics experience without the need to master DirectX. It really is the easy 3D approach.



Bitmap Coding and Metatdata in WPF


Having looked at working with raw pixel data we turn our attention to formattted image files and how to code and decode both the pixel data and the meta data they contain.

 


Other Articles

<ASIN:1430210842>

<ASIN:0672328917>



Last Updated ( Monday, 01 August 2011 )