Ejemplo n.º 1
0
        /// <summary>
        /// Starts a task that waits for the next rendering from Chrome.
        /// Chrome also renders the page loading, so if you want to see a complete rendering,
        /// only start this task once your page is loaded (which you can detect via FrameLoadEnd
        /// or your own heuristics based on evaluating JavaScript).
        /// It is your responsibility to dispose the returned Bitmap.
        /// The bitmap size is determined by the Size property set earlier.
        /// </summary>
        /// <param name="ignoreExistingScreenshot">Ignore existing bitmap (if any) and return the next avaliable bitmap</param>
        /// /// <param name="blend">Choose which bitmap to retrieve, choose <see cref="PopupBlending.Blend"/> for a merged bitmap.</param>
        /// <returns>Task&lt;Bitmap&gt;.</returns>
        public Task <Bitmap> ScreenshotAsync(bool ignoreExistingScreenshot = false, PopupBlending blend = PopupBlending.Main)
        {
            // Try our luck and see if there is already a screenshot, to save us creating a new thread for nothing.
            var screenshot = ScreenshotOrNull(blend);

            var completionSource = new AsyncTaskCompletionSource <Bitmap>();

            if (screenshot == null || ignoreExistingScreenshot)
            {
                EventHandler <OnPaintEventArgs> paint = null; // otherwise we cannot reference ourselves in the anonymous method below

                paint = (sender, e) =>
                {
                    // Chromium has rendered.  Tell the task about it.
                    Paint -= paint;

                    completionSource.TrySetResultAsync(ScreenshotOrNull());
                };

                Paint += paint;
            }
            else
            {
                completionSource.TrySetResultAsync(screenshot);
            }

            return(completionSource.Task);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Immediately returns a copy of the last rendering from Chrome,
        /// or null if no rendering has occurred yet.
        /// Chrome also renders the page loading, so if you want to see a complete rendering,
        /// only start this task once your page is loaded (which you can detect via FrameLoadEnd
        /// or your own heuristics based on evaluating JavaScript).
        /// It is your responsibility to dispose the returned Bitmap.
        /// The bitmap size is determined by the Size property set earlier.
        /// </summary>
        /// <param name="blend">Choose which bitmap to retrieve, choose <see cref="PopupBlending.Blend"/> for a merged bitmap.</param>
        /// <returns>Bitmap.</returns>
        public Bitmap ScreenshotOrNull(PopupBlending blend = PopupBlending.Main)
        {
            lock (BitmapLock)
            {
                if (blend == PopupBlending.Main)
                {
                    return(BitmapBuffer.CreateBitmap());
                }

                if (blend == PopupBlending.Popup)
                {
                    return(PopupOpen ? PopupBuffer.CreateBitmap() : null);
                }


                var bitmap = BitmapBuffer.CreateBitmap();

                if (PopupOpen && bitmap != null)
                {
                    var popup = PopupBuffer.CreateBitmap();
                    if (popup == null)
                    {
                        return(bitmap);
                    }
                    return(MergeBitmaps(bitmap, popup));
                }

                return(bitmap);
            }
        }
        /// <summary>
        /// Immediately returns a copy of the last rendering from Chrome,
        /// or null if no rendering has occurred yet.
        /// Chrome also renders the page loading, so if you want to see a complete rendering,
        /// only start this task once your page is loaded (which you can detect via FrameLoadEnd
        /// or your own heuristics based on evaluating JavaScript).
        /// It is your responsibility to dispose the returned Bitmap.
        /// The bitmap size is determined by the Size property set earlier.
        /// </summary>
        /// <param name="blend">Choose which bitmap to retrieve, choose <see cref="PopupBlending.Blend"/> for a merged bitmap.</param>
        /// <returns>Bitmap.</returns>
        public Bitmap ScreenshotOrNull(PopupBlending blend = PopupBlending.Main)
        {
            lock (BitmapLock)
            {
                if (blend == PopupBlending.Blend)
                {
                    if (PopupOpen && Bitmap != null && Popup != null)
                    {
                        return(MergeBitmaps(Bitmap, Popup));
                    }

                    return(Bitmap == null ? null : new Bitmap(Bitmap));
                }
                else if (blend == PopupBlending.Popup)
                {
                    if (PopupOpen)
                    {
                        return(Popup == null ? null : new Bitmap(Popup));
                    }

                    return(null);
                }

                return(Bitmap == null ? null : new Bitmap(Bitmap));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Immediately returns a copy of the last rendering from Chrome,
        /// or null if no rendering has occurred yet.
        /// Chrome also renders the page loading, so if you want to see a complete rendering,
        /// only start this task once your page is loaded (which you can detect via FrameLoadEnd
        /// or your own heuristics based on evaluating JavaScript).
        /// It is your responsibility to dispose the returned Bitmap.
        /// The bitmap size is determined by the Size property set earlier.
        /// </summary>
        /// <param name="blend">Choose which bitmap to retrieve, choose <see cref="PopupBlending.Blend"/> for a merged bitmap.</param>
        /// <returns>Bitmap.</returns>
        public Bitmap ScreenshotOrNull(PopupBlending blend = PopupBlending.Main)
        {
            if (RenderHandler == null)
            {
                throw new NullReferenceException("RenderHandler cannot be null. Use DefaultRenderHandler unless implementing your own");
            }

            var renderHandler = RenderHandler as DefaultRenderHandler;

            if (renderHandler == null)
            {
                throw new Exception("ScreenshotOrNull and ScreenshotAsync can only be used in combination with the DefaultRenderHandler");
            }

            lock (renderHandler.BitmapLock)
            {
                if (blend == PopupBlending.Main)
                {
                    return(renderHandler.BitmapBuffer.CreateBitmap());
                }

                if (blend == PopupBlending.Popup)
                {
                    return(renderHandler.PopupOpen ? renderHandler.PopupBuffer.CreateBitmap() : null);
                }


                var bitmap = renderHandler.BitmapBuffer.CreateBitmap();

                if (renderHandler.PopupOpen && bitmap != null)
                {
                    var popup = renderHandler.PopupBuffer.CreateBitmap();
                    if (popup == null)
                    {
                        return(bitmap);
                    }
                    return(MergeBitmaps(bitmap, popup, renderHandler.PopupPosition));
                }

                return(bitmap);
            }
        }
Ejemplo n.º 5
0
        public Task <Bitmap> ScreenshotAsync(bool ignoreExistingScreenshot = false, PopupBlending blend = PopupBlending.Main)
        {
            ThrowExceptionIfDisposed();

            // Try our luck and see if there is already a screenshot, to save us creating a new thread for nothing.
            var screenshot = ScreenshotOrNull(blend);

            var completionSource = new TaskCompletionSource <Bitmap>();

            if (screenshot == null || ignoreExistingScreenshot)
            {
                EventHandler <OnPaintEventArgs> afterPaint = null; // otherwise we cannot reference ourselves in the anonymous method below

                afterPaint = (sender, e) =>
                {
                    // Chromium has rendered.  Tell the task about it.
                    AfterPaint -= afterPaint;

                    //If the user handled the Paint event then we'll throw an exception here
                    //as it's not possible to use ScreenShotAsync as the buffer wasn't updated.
                    if (e.Handled)
                    {
                        completionSource.TrySetException(new InvalidOperationException("OnPaintEventArgs.Handled = true, unable to process request. The buffer has not been updated"));
                    }
                    else
                    {
                        completionSource.TrySetResultAsync(ScreenshotOrNull(blend));
                    }
                };

                AfterPaint += afterPaint;
            }
            else
            {
                completionSource.TrySetResultAsync(screenshot);
            }

            return(completionSource.Task);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Immediately returns a copy of the last rendering from Chrome,
        /// or null if no rendering has occurred yet.
        /// Chrome also renders the page loading, so if you want to see a complete rendering,
        /// only start this task once your page is loaded (which you can detect via FrameLoadEnd
        /// or your own heuristics based on evaluating JavaScript).
        /// It is your responsibility to dispose the returned Bitmap.
        /// The bitmap size is determined by the Size property set earlier.
        /// </summary>
        /// <param name="blend">Choose which bitmap to retrieve, choose <see cref="PopupBlending.Blend"/> for a merged bitmap.</param>
        /// <returns>Bitmap.</returns>
        public Bitmap ScreenshotOrNull(PopupBlending blend = PopupBlending.Main)
        {
            lock (BitmapLock)
            {
                if (blend == PopupBlending.Blend)
                {
                    if (PopupOpen && Bitmap != null && Popup != null)
                    {
                        return MergeBitmaps(Bitmap, Popup);
                    }

                    return Bitmap == null ? null : new Bitmap(Bitmap);
                }
                else if(blend == PopupBlending.Popup)
                {
                    if (PopupOpen)
                    {
                        return Popup == null ? null : new Bitmap(Popup);
                    }

                    return null;
                }

                return Bitmap == null ? null : new Bitmap(Bitmap);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Starts a task that waits for the next rendering from Chrome.
        /// Chrome also renders the page loading, so if you want to see a complete rendering,
        /// only start this task once your page is loaded (which you can detect via FrameLoadEnd
        /// or your own heuristics based on evaluating JavaScript).
        /// It is your responsibility to dispose the returned Bitmap.
        /// The bitmap size is determined by the Size property set earlier.
        /// </summary>
        /// <param name="ignoreExistingScreenshot">Ignore existing bitmap (if any) and return the next avaliable bitmap</param>
        /// /// <param name="blend">Choose which bitmap to retrieve, choose <see cref="PopupBlending.Blend"/> for a merged bitmap.</param>
        /// <returns>Task&lt;Bitmap&gt;.</returns>
        public Task<Bitmap> ScreenshotAsync(bool ignoreExistingScreenshot = false, PopupBlending blend = PopupBlending.Main)
        {
            // Try our luck and see if there is already a screenshot, to save us creating a new thread for nothing.
            var screenshot = ScreenshotOrNull(blend);

            var completionSource = new TaskCompletionSource<Bitmap>();

            if (screenshot == null || ignoreExistingScreenshot)
            {
                EventHandler newScreenshot = null; // otherwise we cannot reference ourselves in the anonymous method below

                newScreenshot = (sender, e) =>
                {
                    // Chromium has rendered.  Tell the task about it.
                    NewScreenshot -= newScreenshot;

                    completionSource.TrySetResultAsync(ScreenshotOrNull());
                };

                NewScreenshot += newScreenshot;
            }
            else
            {
                completionSource.TrySetResultAsync(screenshot);
            }

            return completionSource.Task;
        }