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