BitmapSource CreateBitmapSource( Device device, StreamType stream )
        {
            var width = device.GetStreamWidth( stream );
            var height = device.GetStreamHeight( stream );
            var format = device.GetStreamFormat( stream );

            var pixelFormat = PixelFormats.Rgb24;
            if ( format == FormatType.y8 ) {
                pixelFormat = PixelFormats.Gray8;
            }
            else if ( format == FormatType.z16 ) {
                pixelFormat = PixelFormats.Gray16;
            }

            var bpp = pixelFormat.BitsPerPixel / 8;

            return BitmapSource.Create( width, height, 96, 96, pixelFormat, null,
                device.GetFrameData( stream ), width * height * bpp, width * bpp );
        }