public Bitmap Capture() { if (_dupl == null) { _dupl = new DesktopDuplicator(_rectangle, _includeCursor, _monitor); } try { return(_dupl.Capture()); } catch { _dupl?.Dispose(); _dupl = new DesktopDuplicator(_rectangle, _includeCursor, _monitor); try { return(_dupl.Capture()); } catch { return(new Bitmap(Width, Height)); } } }
void Reinit() { _dupl?.Dispose(); _dupl = new DesktopDuplicator(_rectangle, _includeCursor, _monitor) { Timeout = Timeout }; }
public Bitmap Capture() { try { return(_dupl.Capture()); } catch { try { _dupl?.Dispose(); _dupl = new DesktopDuplicator(WindowProvider.DesktopRectangle, _includeCursor, _monitor); return(_dupl.Capture()); } catch { return(new Bitmap(Width, Height)); } } }
/// <summary> /// Gets the next screen frame. DISPOSE of the bitmap when you're done using it! /// </summary> /// <returns></returns> public static Bitmap GetNextFrame() { if (!isInitialized) { Initialize(); } try { DesktopFrame frame = desktopDuplicator.GetLatestFrame(); if (frame != null) { Bitmap frameBitmap = frame.DesktopImage; return(frameBitmap); } } catch (Exception) { desktopDuplicator.Dispose(); desktopDuplicator = new DesktopDuplicator(0); Debug.WriteLine("Exception in DesktopDuplication API occurred. User probably switched windows"); } return(null); }
public void Run(CancellationToken token) { if (IsRunning) { throw new Exception(nameof(DesktopDuplicatorReader) + " is already running!"); } IsRunning = true; _log.Debug("Started Desktop Duplication Reader."); try { BitmapData bitmapData = new BitmapData(); while (!token.IsCancellationRequested) { var frame = _retryPolicy.Execute <DesktopFrame>(GetNextFrame); TraceFrameDetails(frame); if (frame == null) { //there was a timeout before there was the next frame, simply retry! continue; } var image = frame.DesktopImage; image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb, bitmapData); lock (SpotSet.Lock) { var useLinearLighting = Settings.UseLinearLighting; var imageRectangle = new Rectangle(0, 0, image.Width, image.Height); Parallel.ForEach(SpotSet.Spots , spot => { const int numberOfSteps = 15; int stepx = Math.Max(1, spot.Rectangle.Width / numberOfSteps); int stepy = Math.Max(1, spot.Rectangle.Height / numberOfSteps); int sumR; int sumG; int sumB; int count; if (imageRectangle.Width != SpotSet.ExpectedScreenBound.Width || imageRectangle.Height != SpotSet.ExpectedScreenBound.Height) { //the screen was resized or this is some kind of powersaving state SpotSet.IndicateMissingValues(); return; } GetAverageColorOfRectangularRegion(spot.Rectangle, stepy, stepx, bitmapData, out sumR, out sumG, out sumB, out count); var countInverse = 1f / count; byte finalR, finalG, finalB; ApplyColorCorrections(sumR * countInverse, sumG * countInverse, sumB * countInverse, out finalR, out finalG, out finalB, useLinearLighting , Settings.SaturationTreshold, spot.Red, spot.Green, spot.Blue); spot.SetColor(finalR, finalG, finalB); }); } image.UnlockBits(bitmapData); } } finally { _desktopDuplicator?.Dispose(); _desktopDuplicator = null; _log.Debug("Stopped Desktop Duplication Reader."); IsRunning = false; } }
public void Dispose() { _dupl?.Dispose(); }