This project is read-only.

WriteableBitmap performance quirkyness

Feb 21, 2014 at 3:51 AM
I initialize my WriteableBitmap some where at start of program (I call CreateWriteableBitmap())

When I run my program, I have an wpf Image on form, my theImage.Source = writeableBitmap

I receive a new buffer of image data from a camera, and update the writeableBitmap (see code below)
My original FPS(frames per second) was 32 FPS, but the image was not displaying properly
I originally created WriteableBitmap with PixelFormats.Bgr32 setting
I fixed it to correct format: PixelFormats.Gray16
The Image now displayed correctly
That is ONLY THING I changed.

My FPS dropped from 32 FPS to 16 FPS.
The size of the image did not change, just how it was to be interpreted.
This one change dropped frame rate in half.

My question is why??? (again size of image didn't change just this setting)

(see code below)
        public class NativeMethods
            [DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)]
            public static extern void CopyMemory(IntPtr dest, IntPtr src, int size);

        static System.Windows.Media.Imaging.WriteableBitmap writeableBitmap;

        private void CreateWriteableBitmap()
        ..... misc code .....
// FPS = 32 FPS
        original code --> writeableBitmap = new WriteableBitmap((int)width, (int)height, 96, 96, PixelFormats.Bgr32, null);

// FPS = 16 FPS
        fixed code    --> writeableBitmap = new WriteableBitmap((int)width, (int)height, 96, 96, PixelFormats.Gray16, null);

   theImage.Source = writeableBitmap;

        ..... misc code .....

        private void UpdateImage(ImageArrivedArgs info)
            int width = 1316;
            int height = 1312;
            int stride = width * (((int)BitsPerPixel + 7) / 8);

            byte[] data =;

            GCHandle pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned);
            IntPtr ptr2Data = pinnedArray.AddrOfPinnedObject();


            // Copy the bitmap's data directly to the on-screen buffers
            NativeMethods.CopyMemory(writeableBitmap.BackBuffer, ptr2Data, width * height * 2);

            // Moves the back buffer to the front.
            writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));

Feb 23, 2014 at 8:23 AM
Well, that Gray format is not supported by hardware and involves that it's converted from color to gray. Most cameras support YCrCb or similar where you can just use the Y component for brightness value.
Feb 23, 2014 at 6:54 PM
I had not even considered that aspect!!

Thank you for shedding light on that issue!