Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 6 Jan 2012
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)

Share

About the Author

Jarek Kruza
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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 6 Jan 2012
Article Copyright 2012 by Jarek Kruza
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid