// gets the current frame and is triggered every new frame private void currentFrame(VideoStream vStream) { if (vStream.IsValid && vStream.IsFrameAvailable()) { using (OpenNIWrapper.VideoFrameRef frame = vStream.ReadFrame()) { if (frame.IsValid) { VideoFrameRef.CopyBitmapOptions options = VideoFrameRef.CopyBitmapOptions.None | VideoFrameRef.CopyBitmapOptions.Force24BitRgb; lock (this.m_Bitmap) { try { frame.UpdateBitmap(m_Bitmap, options); Image <Bgra, Byte> colorFrame = new Image <Bgra, Byte>(m_Bitmap); Image <Gray, Int32> xFrame = new Image <Gray, float>(m_Bitmap).Convert <Gray, Int32>(); CameraManager.Instance.SetImages(colorFrame, colorFrame, xFrame, xFrame); } catch (Exception exception) { Console.WriteLine("Exception occurred while updating frame."); Console.WriteLine(exception.Message); } } } } } }
private void OpenNI2_OnNewFrame(VideoStream stream) { if (stream != null && stream.IsValid && stream.IsFrameAvailable()) { using (VideoFrameRef frame = stream.ReadFrame()) { if (frame.IsValid) { VideoFrameRef.CopyBitmapOptions options = VideoFrameRef.CopyBitmapOptions.Force24BitRgb | VideoFrameRef.CopyBitmapOptions.DepthFillShadow; try { frame.UpdateBitmap(this.mBitmap, options); } catch (Exception) { // Happens when Bitmap object is not compatible with returned Frame this.mBitmap = frame.ToBitmap(options); } using (MemoryStream memory = new MemoryStream()) { this.mBitmap.Save(memory, ImageFormat.Bmp); memory.Position = 0; this.mImage = new BitmapImage(); this.mImage.BeginInit(); this.mImage.StreamSource = memory; this.mImage.CacheOption = BitmapCacheOption.OnLoad; this.mImage.EndInit(); this.mImage.Freeze(); } this.Dispatcher.Invoke((Action)(() => { this.ImgDepth.Source = this.mImage; })); } } } }
private void CurrentSensorOnNewFrame(VideoStream videoStream) { if (videoStream.IsValid && videoStream.IsFrameAvailable()) { using (VideoFrameRef frame = videoStream.ReadFrame()) { if (frame.IsValid) { VideoFrameRef.CopyBitmapOptions options = VideoFrameRef.CopyBitmapOptions.Force24BitRgb | VideoFrameRef.CopyBitmapOptions.DepthFillShadow; if (this.cb_invert.Checked) { options |= VideoFrameRef.CopyBitmapOptions.DepthInvert; } if (this.cb_equal.Checked) { options |= VideoFrameRef.CopyBitmapOptions.DepthHistogramEqualize; } if (this.cb_fill.Checked) { options |= videoStream.Mirroring ? VideoFrameRef.CopyBitmapOptions.DepthFillRigthBlack : VideoFrameRef.CopyBitmapOptions.DepthFillLeftBlack; } lock (this.bitmap) { /////////////////////// Instead of creating a bitmap object for each frame, you can simply /////////////////////// update one you have. Please note that you must be very careful /////////////////////// with multi-thread situations. try { frame.UpdateBitmap(this.bitmap, options); } catch (Exception) { // Happens when our Bitmap object is not compatible with returned Frame this.bitmap = frame.ToBitmap(options); } /////////////////////// END NOTE /////////////////////// You can always use .toBitmap() if you dont want to /////////////////////// clone image later and be safe when using it in multi-thread situations /////////////////////// This is little slower, but easier to handle // bitmap = frame.toBitmap(options); /////////////////////// END NOTE if (this.cb_mirrorSoft.Checked) { this.bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX); } } ///////////////////// You can simply pass the newly created/updated image to a ///////////////////// PictureBox right here instead of drawing it with Graphic object // this.BeginInvoke(new MethodInvoker(delegate() // { // if (!pb_image.Visible) // pb_image.Visible = true; // if (bitmap == null) // return; // lock (bitmap) // this.BeginInvoke happens on UI Thread so it is better to always keep this lock in place // { // if (pb_image.Image != null) // pb_image.Image.Dispose(); // /////////////////////// If you want to use one bitmap object for all frames, the // /////////////////////// best way to prevent and multi-thread access problems // /////////////////////// is to clone the bitmap each time you want to send it to PictureBox // pb_image.Image = new Bitmap(bitmap, bitmap.Size); // /////////////////////// END NOTE // /////////////////////// If you only use toBitmap() method. you can simply skip the // /////////////////////// cloning process. It is perfectly thread-safe. // //pb_image.Image = bitmap; // /////////////////////// END NOTE // pb_image.Refresh(); // } // })); ///////////////////// END NOTE if (!this.pb_image.Visible) { this.Invalidate(); } } } } }
private void CurrentSensorOnNewFrame(VideoStream vStream) { try { if (vStream.IsValid && vStream.IsFrameAvailable() && !_isIdle) { using (var frame = vStream.ReadFrame()) { if (frame.IsValid) { lock (_bitmap) { try { frame.UpdateBitmap(_bitmap, _renderOptions); } catch (Exception) { _bitmap = frame.ToBitmap(_renderOptions); } var position = new Rectangle(new Point(0, 0), _bitmap.Size); if (_currentCropping == Rectangle.Empty) { _currentCropping = position; } if (Settings.Default.SmartCam) { if (_activeUserId > 0) { position.X = (int)(_activePosition.X * _bitmap.Size.Width); position.Width = (int)(_activePosition.Width * _bitmap.Size.Width); position.Y = (int)(_activePosition.Y * _bitmap.Size.Height); position.Height = (int)(_activePosition.Height * _bitmap.Size.Height); position.Width = (int) ((decimal)_bitmap.Size.Width / _bitmap.Size.Height * position.Height); position.X -= position.Width / 2; position.X = Math.Max(position.X, 0); position.X = Math.Min(position.X, _bitmap.Size.Width - position.Width); position.Y = Math.Max(position.Y, 0); position.Y = Math.Min(position.Y, _bitmap.Size.Height - position.Height); } } if (_currentCropping != position) { if (Math.Abs(position.X - _currentCropping.X) > 8 || Math.Abs(position.Width - _currentCropping.Width) > 5) { _currentCropping.X += Math.Min( position.X - _currentCropping.X, _bitmap.Size.Width / 50); _currentCropping.Width += Math.Min( position.Width - _currentCropping.Width, _bitmap.Size.Width / 25); } if (Math.Abs(position.Y - _currentCropping.Y) > 8 || Math.Abs(position.Height - _currentCropping.Height) > 5) { _currentCropping.Y += Math.Min( position.Y - _currentCropping.Y, _bitmap.Size.Height / 50); _currentCropping.Height += Math.Min( position.Height - _currentCropping.Height, _bitmap.Size.Height / 25); } } if (_currentCropping.Size != _bitmap.Size) { using (var g = Graphics.FromImage(_bitmap)) { if (_currentCropping != Rectangle.Empty) { g.DrawImage( _bitmap, new Rectangle(new Point(0, 0), _bitmap.Size), _currentCropping, GraphicsUnit.Pixel); } g.Save(); } } if (_softMirror) { _bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX); } } if (!_isIdle) { _broadcaster.SendBitmap(_bitmap); } BeginInvoke( (Action) delegate { if (!_isIdle) { lock (_bitmap) { pb_image.Image?.Dispose(); pb_image.Image = new Bitmap(_bitmap, pb_image.Size); pb_image.Refresh(); } } }); } } } } catch { var newBitmap = new Bitmap(640, 480); if (!_isIdle) { _broadcaster.SendBitmap(newBitmap); } BeginInvoke( (Action) delegate { if (!_isIdle) { lock (_bitmap) { pb_image.Image?.Dispose(); pb_image.Image = newBitmap; pb_image.Refresh(); } } }); } }
private void CurrentSensorOnNewFrame(VideoStream vStream) { if (vStream.IsValid && vStream.IsFrameAvailable() && !this.isIdle) { using (VideoFrameRef frame = vStream.ReadFrame()) { if (frame.IsValid) { lock (this.bitmap) { try { frame.UpdateBitmap(this.bitmap, this.renderOptions); } catch (Exception) { this.bitmap = frame.ToBitmap(this.renderOptions); } Rectangle position = new Rectangle(new Point(0, 0), this.bitmap.Size); if (this.currentCropping == Rectangle.Empty) { this.currentCropping = position; } if (Settings.Default.SmartCam) { if (this.activeUserId > 0) { position.X = (int)(this.activePosition.X * this.bitmap.Size.Width); position.Width = (int)(this.activePosition.Width * this.bitmap.Size.Width); position.Y = (int)(this.activePosition.Y * this.bitmap.Size.Height); position.Height = (int)(this.activePosition.Height * this.bitmap.Size.Height); position.Width = (int) (((Decimal)this.bitmap.Size.Width / this.bitmap.Size.Height) * position.Height); position.X -= (position.Width / 2); position.X = Math.Max(position.X, 0); position.X = Math.Min(position.X, this.bitmap.Size.Width - position.Width); position.Y = Math.Max(position.Y, 0); position.Y = Math.Min(position.Y, this.bitmap.Size.Height - position.Height); } } if (this.currentCropping != position) { if (Math.Abs(position.X - this.currentCropping.X) > 8 || Math.Abs(position.Width - this.currentCropping.Width) > 5) { this.currentCropping.X += Math.Min( position.X - this.currentCropping.X, this.bitmap.Size.Width / 50); this.currentCropping.Width += Math.Min( position.Width - this.currentCropping.Width, this.bitmap.Size.Width / 25); } if (Math.Abs(position.Y - this.currentCropping.Y) > 8 || Math.Abs(position.Height - this.currentCropping.Height) > 5) { this.currentCropping.Y += Math.Min( position.Y - this.currentCropping.Y, this.bitmap.Size.Height / 50); this.currentCropping.Height += Math.Min( position.Height - this.currentCropping.Height, this.bitmap.Size.Height / 25); } } if (this.currentCropping.Size != this.bitmap.Size) { using (Graphics g = Graphics.FromImage(this.bitmap)) { if (this.currentCropping != Rectangle.Empty) { g.DrawImage( this.bitmap, new Rectangle(new Point(0, 0), this.bitmap.Size), this.currentCropping, GraphicsUnit.Pixel); } g.Save(); } } if (this.softMirror) { this.bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX); } } if (!this.isIdle) { this.broadcaster.SendBitmap(this.bitmap); } this.BeginInvoke( (Action) delegate { if (!this.isIdle) { lock (this.bitmap) { if (this.pb_image.Image != null) { this.pb_image.Image.Dispose(); } this.pb_image.Image = new Bitmap(this.bitmap, this.pb_image.Size); this.pb_image.Refresh(); } } }); } } } }