/* * private Bitmap CreateBitmap(byte[] data) * { * // Do the magic to create a bitmap * int stride = _camWidth * _bytesPerPixel; * GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); * int scan0 = (int)handle.AddrOfPinnedObject(); * scan0 += (_camHeight - 1) * stride; * Bitmap b = new Bitmap(_camWidth, _camHeight, -stride, PixelFormat.Format24bppRgb, (IntPtr)scan0); * * // Now you can free the handle * handle.Free(); * * return b; * } */ /* * private void CreateImageBitmap(XnMImageMetaData imageMd, out InteropBitmap interopBitmap, out Bitmap bitmap) * { * var format = MapPixelFormat(imageMd.PixelFormat); * var bmpWidth = (int)imageMd.FullXRes; * var bmpHeight = (int)imageMd.FullYRes; * * var numBytes = (uint)(bmpWidth * bmpHeight * format.BitsPerPixel / 8); * var stride = (bmpWidth * format.BitsPerPixel / 8); * * var section = NativeMethods.CreateFileMapping(NativeMethods.INVALID_HANDLE_VALUE, IntPtr.Zero, NativeMethods.PAGE_READWRITE, 0, numBytes, null); * var map = NativeMethods.MapViewOfFile(section, NativeMethods.FILE_MAP_ALL_ACCESS, 0, 0, numBytes); * * interopBitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(section, * bmpWidth, bmpHeight, format, stride, 0); * * bitmap = new Bitmap(bmpWidth, bmpHeight, stride, PixelFormat.Format24bppRgb, map); * * NativeMethods.CloseHandle(section); * } */ private static PixelFormat MapPixelFormat(XnMPixelFormat xnMPixelFormat) { switch (xnMPixelFormat) { case XnMPixelFormat.Grayscale8Bit: return(PixelFormats.Gray8); case XnMPixelFormat.Grayscale16Bit: return(PixelFormats.Gray16); case XnMPixelFormat.Rgb24: return(PixelFormats.Rgb24); case XnMPixelFormat.Yuv422: default: throw new NotSupportedException(); } }
/* private Bitmap CreateBitmap(byte[] data) { // Do the magic to create a bitmap int stride = _camWidth * _bytesPerPixel; GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); int scan0 = (int)handle.AddrOfPinnedObject(); scan0 += (_camHeight - 1) * stride; Bitmap b = new Bitmap(_camWidth, _camHeight, -stride, PixelFormat.Format24bppRgb, (IntPtr)scan0); // Now you can free the handle handle.Free(); return b; } */ /* private void CreateImageBitmap(XnMImageMetaData imageMd, out InteropBitmap interopBitmap, out Bitmap bitmap) { var format = MapPixelFormat(imageMd.PixelFormat); var bmpWidth = (int)imageMd.FullXRes; var bmpHeight = (int)imageMd.FullYRes; var numBytes = (uint)(bmpWidth * bmpHeight * format.BitsPerPixel / 8); var stride = (bmpWidth * format.BitsPerPixel / 8); var section = NativeMethods.CreateFileMapping(NativeMethods.INVALID_HANDLE_VALUE, IntPtr.Zero, NativeMethods.PAGE_READWRITE, 0, numBytes, null); var map = NativeMethods.MapViewOfFile(section, NativeMethods.FILE_MAP_ALL_ACCESS, 0, 0, numBytes); interopBitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(section, bmpWidth, bmpHeight, format, stride, 0); bitmap = new Bitmap(bmpWidth, bmpHeight, stride, PixelFormat.Format24bppRgb, map); NativeMethods.CloseHandle(section); } */ private static PixelFormat MapPixelFormat(XnMPixelFormat xnMPixelFormat) { switch (xnMPixelFormat) { case XnMPixelFormat.Grayscale8Bit: return PixelFormats.Gray8; case XnMPixelFormat.Grayscale16Bit: return PixelFormats.Gray16; case XnMPixelFormat.Rgb24: return PixelFormats.Rgb24; case XnMPixelFormat.Yuv422: default: throw new NotSupportedException(); } }