views:

33

answers:

1

I would like to do some processing of images in a WPF application. However, I would like to modify the pixels of a BitmapSource in memory at runtime.

I'm currently managing to do this using 'unsafe' code against an old fashioned System.Drawing.Bitmap and it works a treat (lock a working area, fiddle with the pixels) job done. The approach is described in this post: http://blogs.msdn.com/b/ericgu/archive/2007/06/20/lost-column-2-unsafe-image-processing.aspx

To get this working in WPF I'm then creating a WPF BitmapSource using this approach:

    BitmapSource destination;
    IntPtr hBitmap = bitmap.GetHbitmap();
    BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions();
    destination = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, sizeOptions);
    destination.Freeze();
    return destination;

However, this creates a lot of copies in memory and I really want to get in there and fiddle with the underlying bits inside the BitmapSource just like EricGu showed in the Bitmap example. Is this possible?

I realise that PixelShaders can probably do this but this is an academic exercise involving multiple threads (which is supported when editing a bitmap in unsafe mode).

Thanks in advance

+3  A: 

I'll tell you as much as I know.

First you appear to already know about Freezing bitmaps before giving them back to the main thread. That's a good start.

I have once tried to reduce copying by deriving a new implementation from BitmapSource, and override the virtual functions that copy your data out of the bitmap. It worked but produced massive memory leaks. Never figured it out.

You can also of course create a BitmapSource directly from an array of bytes (and lose overhead of the GDI bitmap). msdn docs

Have you considered a WriteableBitmap? That was the WPF team's response to the common complaint about all of the copying.

Edit: MSDN says explicitly (in WritableBitmap.BackBuffer documentation) that it can be used from a background thread:

You can pass the BackBuffer pointer to external components and other threads for processing, but if you do, you must provide you own thread coordination. In particular, you must ensure that the UI thread specifies changed areas by calling the AddDirtyRect method, and that the UI thread unlocks the buffer by calling the Unlock method.

So if you let the UI thread aquire the lock and the pointer, you can let a worker thread write the pixels.

jdv
Thanks, I did know about the WriteableBitmap but this can only be updated by a single thread IIRC. I really want to play with the image in a background thread which I don' think is possible with WritableBitmap, right?
ConfusedNoob
Yes that is possible (see edit).
jdv