IanG on Tap

Ian Griffiths in Weblog Form (RSS 2.0)

Blog Navigation

March (2014)

(1 item)

January (2014)

(2 items)

November (2013)

(2 items)

July (2013)

(4 items)

April (2013)

(1 item)

February (2013)

(6 items)

September (2011)

(2 items)

November (2010)

(4 items)

September (2010)

(1 item)

August (2010)

(4 items)

July (2010)

(2 items)

September (2009)

(1 item)

June (2009)

(1 item)

April (2009)

(1 item)

November (2008)

(1 item)

October (2008)

(1 item)

September (2008)

(1 item)

July (2008)

(1 item)

June (2008)

(1 item)

May (2008)

(2 items)

April (2008)

(2 items)

March (2008)

(5 items)

January (2008)

(3 items)

December (2007)

(1 item)

November (2007)

(1 item)

October (2007)

(1 item)

September (2007)

(3 items)

August (2007)

(1 item)

July (2007)

(1 item)

June (2007)

(2 items)

May (2007)

(8 items)

April (2007)

(2 items)

March (2007)

(7 items)

February (2007)

(2 items)

January (2007)

(2 items)

November (2006)

(1 item)

October (2006)

(2 items)

September (2006)

(1 item)

June (2006)

(2 items)

May (2006)

(4 items)

April (2006)

(1 item)

March (2006)

(5 items)

January (2006)

(1 item)

December (2005)

(3 items)

November (2005)

(2 items)

October (2005)

(2 items)

September (2005)

(8 items)

August (2005)

(7 items)

June (2005)

(3 items)

May (2005)

(7 items)

April (2005)

(6 items)

March (2005)

(1 item)

February (2005)

(2 items)

January (2005)

(5 items)

December (2004)

(5 items)

November (2004)

(7 items)

October (2004)

(3 items)

September (2004)

(7 items)

August (2004)

(16 items)

July (2004)

(10 items)

June (2004)

(27 items)

May (2004)

(15 items)

April (2004)

(15 items)

March (2004)

(13 items)

February (2004)

(16 items)

January (2004)

(15 items)

Blog Home

RSS 2.0

Writing

Programming C# 5.0

Programming WPF

Other Sites

Interact Software

Creating Images from Raw Pixel Data in WPF

Friday 10 March, 2006, 11:45 PM

One of the restrictions I've often run into with GDI+ is that it makes life awkward if you want to work with raw pixel values. (E.g., for generating images from scratch or performing image processing.) It's possible, your two options being (1) the GetPixel and SetPixel methods, or (2) LockBits and UnlockBits. The first option is abominably slow if you're dealing with large volumes of pixels - you call the methods for every single pixel. The second approach requires unsafe code, which rules out partial trust scenarios, and leaves me with an uneasy tense feeling - I prefer to write verifiable code whenever possible.

In WPF, things are better.

WPF lets you pass in a big array of pixel values to create a new BitmapSource. It also offers CopyPixel methods that copies pixel data out of the bitmap into an array.

This means you can write verifiable code that generates or process pixel data efficiently. Here's a very simple example:

double dpi = 96;
int width = 128;
int height = 128;
byte[] pixelData = new byte[width*height];

for (int y = 0; y < height; ++y)
{
    int yIndex = y * width;
    for (int x = 0; x < width; ++x)
    {
        pixelData[x + yIndex] = (byte) (x + y);
    }
}

BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi,
    PixelFormats.Gray8, null, pixelData, width);

This creates a BitmapSource of a linear gradient fill from black to white from top left to bottom right. Yes, there are much easier ways to do that in WPF, but the point here is to illustrate how you can take raw pixel data and turn it into a BitmapSource.

Once you've got a BitmapSource you can then use it, say, to set the Source property of an Image element in order to display the bitmap:

<Image x:Name="imageDisplay" />

...

imageDisplay.Source = bmpSource;

And of course you can use with an ImageBrush, or do all the other things you can do with bitmaps in WPF.

Copyright © 2002-2013, Interact Software Ltd. Content by Ian Griffiths. Please direct all Web site inquiries to webmaster@interact-sw.co.uk