Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / C# 4.0

Getting the most of Kinect SDK in C# - Part 2 of ?: ImageStreams

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
6 Jan 2012Ms-PL4 min read 36.3K   1.2K   12  
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;
        }
        #endregion


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

        public InteropBitmap InteropBitmap { get; private set; }
        #endregion


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


        #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);
        #endregion
    }
}

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.

License

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


Written By
Flextronics
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.

Comments and Discussions