private void colorStream_onNewFrame(VideoStream vStream) { if (vStream.isValid && vStream.isFrameAvailable()) { using (VideoFrameRef frame = vStream.readFrame()) { if (frame.isValid) { if (this.colorBitmap == null) { this.colorBitmap = new Bitmap(1, 1); } lock (this.colorBitmap) { try { frame.updateBitmap(this.colorBitmap, VideoFrameRef.copyBitmapOptions.Force24BitRGB); } catch (Exception) { this.colorBitmap = frame.toBitmap(VideoFrameRef.copyBitmapOptions.Force24BitRGB); } } this.BeginInvoke( (Action)delegate { lock (this.colorBitmap) { if (this.p_image.Image != null) { this.p_image.Image.Dispose(); } this.p_image.Image = new Bitmap(this.colorBitmap); this.p_image.Refresh(); } }); } } } }
private void depthStream_onNewFrame(VideoStream vStream) { if (vStream.isValid && vStream.isFrameAvailable()) { using (VideoFrameRef frame = vStream.readFrame()) { if (frame.isValid) { if (this.depthBitmap == null) { this.depthBitmap = new Bitmap(1, 1); } lock (this.depthBitmap) { try { frame.updateBitmap( this.depthBitmap, VideoFrameRef.copyBitmapOptions.Force24BitRGB | VideoFrameRef.copyBitmapOptions.DepthInvert | VideoFrameRef.copyBitmapOptions.DepthFillShadow | VideoFrameRef.copyBitmapOptions.DepthHistogramEqualize | VideoFrameRef.copyBitmapOptions.DepthFillRigthBlack); } catch (Exception) { this.depthBitmap = frame.toBitmap( VideoFrameRef.copyBitmapOptions.Force24BitRGB | VideoFrameRef.copyBitmapOptions.DepthInvert | VideoFrameRef.copyBitmapOptions.DepthFillShadow | VideoFrameRef.copyBitmapOptions.DepthHistogramEqualize | VideoFrameRef.copyBitmapOptions.DepthFillRigthBlack); } } this.BeginInvoke( (Action)delegate { lock (this.depthBitmap) { if (this.p_depth.Image != null) { this.p_depth.Image.Dispose(); } this.p_depth.Image = new Bitmap(this.depthBitmap); this.p_depth.Refresh(); } }); } } } }
void currentSensor_onNewFrame(VideoStream vStream) { if (vStream.isValid && vStream.isFrameAvailable()) { using (VideoFrameRef frame = vStream.readFrame()) { if (frame.isValid) { VideoFrameRef.copyBitmapOptions options = VideoFrameRef.copyBitmapOptions.Force24BitRGB | VideoFrameRef.copyBitmapOptions.DepthFillShadow; if (cb_invert.Checked) options |= VideoFrameRef.copyBitmapOptions.DepthInvert; if (cb_equal.Checked) options |= VideoFrameRef.copyBitmapOptions.DepthHistogramEqualize; if (cb_fill.Checked) options |= (vStream.Mirroring) ? VideoFrameRef.copyBitmapOptions.DepthFillRigthBlack : VideoFrameRef.copyBitmapOptions.DepthFillLeftBlack; lock (bitmap) { try { frame.updateBitmap(bitmap, options); } catch (Exception) { bitmap = frame.toBitmap(options); } } this.BeginInvoke(new MethodInvoker(delegate() { lock (bitmap) { if (pb_image.Image != null) pb_image.Image.Dispose(); pb_image.Image = new Bitmap(bitmap, pb_image.Size); pb_image.Refresh(); } })); } } } }
void depthSensor_onNewFrame(VideoStream vStream) { if (!vStream.isValid || !vStream.isFrameAvailable()) return; VideoFrameRef frame = vStream.readFrame(); if (!frame.isValid) return; lock (depthMutex) { depthFrame = frame; } }
void colorSensor_onNewFrame(VideoStream vStream) { if (!vStream.isValid || !vStream.isFrameAvailable()) return; VideoFrameRef frame = vStream.readFrame(); if (!frame.isValid) return; lock (colorMutex) { colorFrame = frame; } }
void currentSensor_onNewFrame(VideoStream vStream) { if (vStream.isValid && vStream.isFrameAvailable() && !isIdle) { using (VideoFrameRef frame = vStream.readFrame()) { if (frame.isValid) { lock (bitmap) { try { frame.updateBitmap(bitmap, renderOptions); } catch (Exception) { bitmap = frame.toBitmap(renderOptions); } } Rectangle ActivePosition = new Rectangle(new Point(0, 0), bitmap.Size); if (currentCropping == Rectangle.Empty) currentCropping = ActivePosition; if (Properties.Settings.Default.SmartCam) { if (ActiveUserId > 0) { ActivePosition.X = (int)(this.ActivePosition.X * bitmap.Size.Width); ActivePosition.Width = (int)(this.ActivePosition.Width * bitmap.Size.Width); ActivePosition.Y = (int)(this.ActivePosition.Y * bitmap.Size.Height); ActivePosition.Height = (int)(this.ActivePosition.Height * bitmap.Size.Height); ActivePosition.Width = (int)(((Decimal)bitmap.Size.Width / bitmap.Size.Height) * ActivePosition.Height); ActivePosition.X -= (ActivePosition.Width / 2); ActivePosition.X = Math.Max(ActivePosition.X, 0); ActivePosition.X = Math.Min(ActivePosition.X, bitmap.Size.Width - ActivePosition.Width); ActivePosition.Y = Math.Max(ActivePosition.Y, 0); ActivePosition.Y = Math.Min(ActivePosition.Y, bitmap.Size.Height - ActivePosition.Height); } } if (currentCropping != ActivePosition) { if (Math.Abs(ActivePosition.X - currentCropping.X) > 8 || Math.Abs(ActivePosition.Width - currentCropping.Width) > 5) { currentCropping.X += Math.Min(ActivePosition.X - currentCropping.X, bitmap.Size.Width / 50); currentCropping.Width += Math.Min(ActivePosition.Width - currentCropping.Width, bitmap.Size.Width / 25); } if (Math.Abs(ActivePosition.Y - currentCropping.Y) > 8 || Math.Abs(ActivePosition.Height - currentCropping.Height) > 5) { currentCropping.Y += Math.Min(ActivePosition.Y - currentCropping.Y, bitmap.Size.Height / 50); currentCropping.Height += Math.Min(ActivePosition.Height - currentCropping.Height, bitmap.Size.Height / 25); } } lock (bitmap) { if (currentCropping.Size != bitmap.Size) { using (Graphics 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); this.BeginInvoke((Action)delegate { if (!isIdle) lock (bitmap) { if (pb_image.Image != null) pb_image.Image.Dispose(); pb_image.Image = new Bitmap(bitmap, pb_image.Size); pb_image.Refresh(); } }); } } } }
void currentSensor_onNewFrame(VideoStream vStream) { if (vStream.isValid && vStream.isFrameAvailable()) { using (VideoFrameRef frame = vStream.readFrame()) { if (frame.isValid) { VideoFrameRef.copyBitmapOptions options = VideoFrameRef.copyBitmapOptions.Force24BitRGB | VideoFrameRef.copyBitmapOptions.DepthFillShadow; if (cb_invert.Checked) options |= VideoFrameRef.copyBitmapOptions.DepthInvert; if (cb_equal.Checked) options |= VideoFrameRef.copyBitmapOptions.DepthHistogramEqualize; if (cb_fill.Checked) options |= (vStream.Mirroring) ? VideoFrameRef.copyBitmapOptions.DepthFillRigthBlack : VideoFrameRef.copyBitmapOptions.DepthFillLeftBlack; lock (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(bitmap, options); } catch (Exception) // Happens when our Bitmap object is not compatible with returned Frame { 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 (cb_mirrorSoft.Checked) 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 (!pb_image.Visible) this.Invalidate(); } } } }