/// <summary> /// Converts a color frame to a System.Media.Imaging.BitmapSource. /// </summary> /// <param name="frame">The specified color frame.</param> /// <returns>The specified frame in a System.Media.Imaging.BitmapSource representation of the color frame.</returns> public static BitmapSource ToBitmap(this ColorFrame frame) { if (_bitmap == null) { _width = frame.FrameDescription.Width; _height = frame.FrameDescription.Height; _pixels = new byte[_width * _height * Constants.BYTES_PER_PIXEL]; _bitmap = new WriteableBitmap(_width, _height, Constants.DPI, Constants.DPI, Constants.FORMAT, null); } if (frame.RawColorImageFormat == ColorImageFormat.Bgra) { frame.CopyRawFrameDataToArray(_pixels); } else { frame.CopyConvertedFrameDataToArray(_pixels, ColorImageFormat.Bgra); } _bitmap.Lock(); Marshal.Copy(_pixels, 0, _bitmap.BackBuffer, _pixels.Length); _bitmap.AddDirtyRect(new Int32Rect(0, 0, _width, _height)); _bitmap.Unlock(); return _bitmap; }
/// <summary> /// Converts a color frame to a System.Drawing.Bitmap. /// </summary> /// <param name="frame">A ColorImageFrame generated from a Kinect sensor.</param> /// <returns>The specified frame in a System.Drawing.Bitmap format.</returns> public static Bitmap ToBitmap(this ColorFrame frame) { if (_bitmap == null) { _width = frame.FrameDescription.Width; _height = frame.FrameDescription.Height; _pixels = new byte[_width * _height * Constants.BYTES_PER_PIXEL]; _bitmap = new Bitmap(_width, _height, Constants.FORMAT); } if (frame.RawColorImageFormat == ColorImageFormat.Bgra) { frame.CopyRawFrameDataToArray(_pixels); } else { frame.CopyConvertedFrameDataToArray(_pixels, ColorImageFormat.Bgra); } BitmapData bitmapData = _bitmap.LockBits(new Rectangle(0, 0, _width, _height), ImageLockMode.ReadWrite, _bitmap.PixelFormat); Marshal.Copy(_pixels, 0, bitmapData.Scan0, _pixels.Length); _bitmap.UnlockBits(bitmapData); return _bitmap; }
internal static void CopyToFrameToPixelArray(this ColorFrame colorFrame, ref byte[] pixels) { if (colorFrame.RawColorImageFormat == ColorImageFormat.Bgra) colorFrame.CopyRawFrameDataToArray(pixels); else colorFrame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra); }
/// <summary> /// Serializes a color frame. /// </summary> /// <param name="frame">The specified color frame.</param> /// <returns>A binary representation of the frame.</returns> public static byte[] Serialize(this ColorFrame frame) { Console.WriteLine("In Color Serializing"); if (_colorBitmap == null) { _colorWidth = frame.FrameDescription.Width; _colorHeight = frame.FrameDescription.Height; _colorStride = _colorWidth * Constants.PIXEL_FORMAT.BitsPerPixel / 8; _colorPixels = new byte[frame.FrameDescription.Width * frame.FrameDescription.Height * 4]; //ColorImageFormat.Bgra is 4 byte per pixel _colorBitmap = new WriteableBitmap(_colorWidth, _colorHeight, Constants.DPI, Constants.DPI, Constants.PIXEL_FORMAT, null); } //Console.WriteLine(frame.RawColorImageFormat); //frame.CopyRawFrameDataToArray(_colorPixels); if (frame.RawColorImageFormat == ColorImageFormat.Bgra) { frame.CopyRawFrameDataToArray(_colorPixels); } else { frame.CopyConvertedFrameDataToArray(_colorPixels, ColorImageFormat.Bgra); } _colorBitmap.WritePixels(new Int32Rect(0, 0, _colorWidth, _colorHeight), _colorPixels, _colorStride, 0); return FrameSerializer.CreateBlob(_colorBitmap, Constants.CAPTURE_FILE_COLOR); }
/// <summary> /// Converts a color frame to a System.Drawing.Bitmap. /// </summary> /// <param name="frame">A ColorImageFrame generated from a Kinect sensor.</param> /// <returns>The specified frame in a System.Drawing.Bitmap format.</returns> public static Bitmap ToBitmap(this ColorFrame frame) { int width = frame.FrameDescription.Width; int height = frame.FrameDescription.Height; byte[] pixels = new byte[frame.FrameDescription.LengthInPixels]; if (frame.RawColorImageFormat == ColorImageFormat.Bgra) { frame.CopyRawFrameDataToArray(pixels); } else { frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra); } return pixels.ToBitmap(width, height); }
/// <summary> /// Converts a color frame to a System.Media.ImageSource. /// </summary> /// <param name="frame">The specified color frame.</param> /// <returns>The specified frame in a System.media.ImageSource format.</returns> public static ImageSource ToBitmap(this ColorFrame frame) { int width = frame.FrameDescription.Width; int height = frame.FrameDescription.Height; byte[] pixels = new byte[width * height * ((PixelFormats.Bgr32.BitsPerPixel + 7) / 8)]; if (frame.RawColorImageFormat == ColorImageFormat.Bgra) { frame.CopyRawFrameDataToArray(pixels); } else { frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra); } return pixels.ToBitmap(width, height); }
public static ImageSource ToBitmap(this ColorFrame frame) { int width = frame.FrameDescription.Width; int height = frame.FrameDescription.Height; PixelFormat format = PixelFormats.Bgr32; byte[] pixels = new byte[width * height * ((format.BitsPerPixel + 7) / 8)]; if (frame.RawColorImageFormat == ColorImageFormat.Bgra) { frame.CopyRawFrameDataToArray(pixels); } else { frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra); } int stride = width * format.BitsPerPixel / 8; return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride); }
/// <summary> /// Serializes a color frame. /// </summary> /// <param name="frame">The specified color frame.</param> /// <returns>A binary representation of the frame.</returns> //public static byte[] Serialize(this ColorImageFrame frame) public static byte[] Serialize(this ColorFrame frame) { if (_colorBitmap == null) { //_colorWidth = frame.Width; _colorWidth = frame.FrameDescription.Width; //_colorHeight = frame.Height; _colorHeight = frame.FrameDescription.Height; _colorStride = _colorWidth * Constants.PIXEL_FORMAT.BitsPerPixel / 8; //_colorPixels = new byte[frame.FrameDescription.PixelDataLength]; //_colorPixels = new byte[frame.FrameDescription.LengthInPixels * frame.FrameDescription.BytesPerPixel]; _colorPixels = new byte[frame.FrameDescription.LengthInPixels * 4]; _colorBitmap = new WriteableBitmap(_colorWidth, _colorHeight, Constants.DPI, Constants.DPI, Constants.PIXEL_FORMAT, null); } //frame.CopyPixelDataTo(_colorPixels); //frame.CopyRawFrameDataToArray(_colorPixels); frame.CopyConvertedFrameDataToArray(_colorPixels, ColorImageFormat.Rgba); _colorBitmap.WritePixels(new Int32Rect(0, 0, _colorWidth, _colorHeight), _colorPixels, _colorStride, 0); return FrameSerializer.CreateBlob(_colorBitmap, Constants.CAPTURE_FILE_COLOR); }