I got a problem with the following c# (test-)code:
public static void TestBitmap2ByteArray(BitmapImage bitmap)
{
JpegBitmapEncoder encoder = new
You are accessing the bitmap data by saving it to a stream. This causes overhead. A much better way to do it is by using LockBits. This will give you direct access to the bytes in the image and you can easily access it both as *byte[] and *UInt32[] (note: requires unsafe{}).
If you still need to copy the bytes to a byte array then Marshal.Copy can be used, but if your intent is to make modifications directly to the image you are free to do so. The LockBits link has a sample showing the use of Marshal.Copy.
If you need some samples of how to process picture I have released some sample code on image processing for the Kinect.