This project is read-only.

A small suggestion

Jan 8, 2010 at 8:51 AM


Great project. I am sure this will prove very useful. I have a suggestion for an additional method to add:


/// <summary>
/// Applies the given function to all the pixels of the bitmap in 
/// order to set their color.
/// </summary>
/// <param name="bitmap">The WriteableBitmap.</param>
/// <param name="func">The function to apply.</param>
public static void EachPixel(this WriteableBitmap bitmap, Func<int, int, Color> func)
    int width = bitmap.PixelWidth;
    int height = bitmap.PixelHeight;
    int index = 0;

    for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
                bitmap.SetPixeli(index++, func(x, y));

This method enumerates the bitmap pixels setting the value of each. It is great for removing repeated 'for' loops over the x / y values of the bitmap. 



b.EachPixel((x,y) => Color.FromArgb(255, 0, (byte)(x % 255), (byte)(y % 255)));


Colin E.



Jan 8, 2010 at 9:49 AM

Hi Colin,

thanks for your pleasant words.

This is a great addition and might become very useful and I will integrate it for sure. Why didn't I came up with this earlier? :)
Why have you named it "EachPixel" and not "ForEach"?
BTW, do you have a website or profile anywhere? I'd like to add you to the project's credits.

Thanks again!

René Schulte

Jan 8, 2010 at 10:10 AM

Hi René,

I names it EachPixel because a bitmap is not a collection, so ForEach looked a bit odd to me. However, I don't really have any strong feelings either way.

Here's my website:

Being added to the credits would be great.

One general observation I have is that many of these extension methods fail with array bounds exceptions if the x / y values are out of bounds. This simplifies the implementation, but is not very user friendly. I think most users would expect that if their shape goes outside of the bitmap 'canvas' that it would be clipped.

What are your thoughts on this?

Regard, Colin E.



Jan 8, 2010 at 11:18 AM

Basically the WriteableBitmap is just an array (collection) of pixels and I will add it as "ForEach" cause it's more common for most users. Thanks again!

I left the boundary checks out for better performance and I'm not planning to change that behavior. The caller is responsible for checking it. Actually I had the boundary check implemented for a quick performance test and it had it's little impact. Furthermore their should some more clipping modes then just CLAMP, WRAP for example.
However I think I should add an clip parameter to the methods so the user can choose between fast or safe. :)  Feel free to suggest it via the Issue Tracker or to help me. :)