private void CurrentSensorOnNewFrame(byte[] data, int bytesPerPixel) { try { Bitmap im; lock (bitmapLock) { if (depthBytes != null) { im = new Bitmap(640, 480, PixelFormat.Format24bppRgb); BitmapData bmapdata = im.LockBits( new Rectangle(0, 0, 640, 480), ImageLockMode.WriteOnly, im.PixelFormat); IntPtr ptr = bmapdata.Scan0; for (int i = 0; i < 640 * 480; i++) { var correctedValue = correctedDepth[i]; if (correctedValue == Foreground) { Marshal.Copy(data, i * 4, ptr + i * 3, 3); } else { Marshal.Copy(background, i * 3, ptr + i * 3, 3); } } im.UnlockBits(bmapdata); } else { im = new Bitmap(640, 480, bytesPerPixel == 4 ? PixelFormat.Format32bppRgb: PixelFormat.Format24bppRgb); BitmapData bmapdata = im.LockBits( new Rectangle(0, 0, 640, 480), ImageLockMode.WriteOnly, im.PixelFormat); IntPtr ptr = bmapdata.Scan0; Marshal.Copy(data, 0, ptr, data.Length); im.UnlockBits(bmapdata); } var position = new Rectangle(new Point(0, 0), im.Size); if (_currentCropping == Rectangle.Empty) { _currentCropping = position; } if (_softMirror) { im.RotateFlip(RotateFlipType.RotateNoneFlipX); } if (!_isIdle) { if (im.PixelFormat != PixelFormat.Format24bppRgb) { im = im.Clone(new Rectangle(0, 0, 640, 480), PixelFormat.Format24bppRgb); } _broadcaster.SendBitmap(im); } } BeginInvoke( (Action) delegate { var img = im; if (!_isIdle) { lock (bitmapLock) { if (img.PixelFormat != PixelFormat.DontCare) { pb_image.Image?.Dispose(); pb_image.Image = new Bitmap(img, pb_image.Size); pb_image.Refresh(); } } } }); } catch (ArgumentException e) { GC.Collect(); //memory issue? } catch (Exception e) { if (e is OutOfMemoryException) { GC.Collect(); } var newBitmap = new Bitmap(640, 480, PixelFormat.Format24bppRgb); if (!_isIdle) { _broadcaster.SendBitmap(newBitmap); } BeginInvoke( (Action) delegate { if (!_isIdle) { lock (bitmapLock) { pb_image.Image?.Dispose(); pb_image.Image = newBitmap; pb_image.Refresh(); } } }); } }
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(); } } }); } }