Click here to Skip to main content
12,820,053 members (24,593 online)
Click here to Skip to main content


27 bookmarked
Posted 20 Nov 2011

Getting most of the Kinect SDK in C# - Part 1 of ?: Initialization

, 6 Jan 2012 Ms-PL
A series about experimenting with Kinect for Windows SDK.
using System;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Media;

namespace JK.KinectExperiments.ImageStreamTest

    /// <summary>
    /// InteropBitmapHelper is a helper class meant to ease the scenario of using an interopBitmap.
    /// InteropBitmaps are a construct in WPF which allow update of the image bits in an efficient way.
    /// </summary>
    public class InteropBitmapHelper
        #region ctors
        public InteropBitmapHelper(int width, int height, byte[] imageBits)
            : this(width, height, imageBits, PixelFormats.Bgr32)

        public InteropBitmapHelper(int width, int height, byte[] imageBits, PixelFormat pixelFormat)
            _Width = width;
            _Height = height;
            _PixelFormat = pixelFormat;

            int stride = width * pixelFormat.BitsPerPixel / 8;
            _ColorByteCount = (uint)(height * stride);
            var colorFileMapping = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0x04, 0, _ColorByteCount, null);
            _ImageBits = MapViewOfFile(colorFileMapping, 0xF001F, 0, 0, _ColorByteCount);
            Marshal.Copy(imageBits, 0, _ImageBits, (int)_ColorByteCount);
            InteropBitmap = Imaging.CreateBitmapSourceFromMemorySection(colorFileMapping, width, height, pixelFormat, stride, 0) as InteropBitmap;

        #region public API
        public void UpdateBits(byte[] imageBits)
            Marshal.Copy(imageBits, 0, _ImageBits, (int)_ColorByteCount);

        public InteropBitmap InteropBitmap { get; private set; }

        #region private state
        private int _Width;
        private int _Height;
        private PixelFormat _PixelFormat;
        private IntPtr _ImageBits;
        private uint _ColorByteCount;

        #region DllImports
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr CreateFileMapping(IntPtr hFile,
                                               IntPtr lpFileMappingAttributes,
                                              uint flProtect,
                                              uint dwMaximumSizeHigh,
                                              uint dwMaximumSizeLow,
                                              string lpName);
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject,
                                          uint dwDesiredAccess,
                                          uint dwFileOffsetHigh,
                                          uint dwFileOffsetLow,
                                          uint dwNumberOfBytesToMap);

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Jarek Kruza
Poland Poland
Programming since 10 years old with first commercial app sold in age of 16.

In past got Bachelor's degree in Computer Sciences and worked as Linux administrator and software developer.

Currently slightly over 30 and working as IT Project Manager for Flextronics.

Still coding for fun and/or money.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 6 Jan 2012
Article Copyright 2011 by Jarek Kruza
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid