diff --git a/CefSharp.OffScreen/CefSharp.OffScreen.csproj b/CefSharp.OffScreen/CefSharp.OffScreen.csproj index 26258c384..b23606934 100644 --- a/CefSharp.OffScreen/CefSharp.OffScreen.csproj +++ b/CefSharp.OffScreen/CefSharp.OffScreen.csproj @@ -75,6 +75,7 @@ + diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index eb08cccb4..092687a70 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -4,6 +4,7 @@ using System; using System.Drawing; +using System.Drawing.Imaging; using System.Threading.Tasks; using CefSharp.Internals; @@ -90,6 +91,20 @@ namespace CefSharp.OffScreen /// binding. public bool CanGoBack { get; private set; } /// + /// Gets or sets a value indicating whether the popup just opened. + /// + /// + /// true if popup just opened; otherwise, false. + /// + public bool PopupJustOpened { get; protected set; } + /// + /// Gets or sets a value indicating whether the popup is open. + /// + /// + /// true if popup is opened; otherwise, false. + /// + public bool PopupOpened { get; protected set; } + /// /// A flag that indicates whether the state of the control currently supports the GoForward action (true) or not (false). /// /// true if this instance can go forward; otherwise, false. @@ -106,6 +121,7 @@ namespace CefSharp.OffScreen /// /// The request context. public RequestContext RequestContext { get; private set; } + /// /// Implement and assign to handle events related to JavaScript Dialogs. /// @@ -272,6 +288,21 @@ namespace CefSharp.OffScreen /// public event EventHandler NewScreenshot; + /// + /// Top Left position of the popup. + /// + private Point popupPosition; + + /// + /// Size of the popup. + /// + private Size popupSize; + + /// + /// The popup Bitmap. + /// + public Bitmap Popup { get; set; } + /// /// Create a new OffScreen Chromium Browser /// @@ -279,6 +310,7 @@ namespace CefSharp.OffScreen /// The browser settings to use. If null, the default settings are used. /// See for more details. Defaults to null /// automatically create the underlying Browser + /// Should the popup be blended in the background in the rendering /// Cef::Initialize() failed public ChromiumWebBrowser(string address = "", BrowserSettings browserSettings = null, RequestContext requestContext = null, bool automaticallyCreateBrowser = true) @@ -299,11 +331,13 @@ namespace CefSharp.OffScreen managedCefBrowserAdapter = new ManagedCefBrowserAdapter(this, true); - if(automaticallyCreateBrowser) + if (automaticallyCreateBrowser) { CreateBrowser(IntPtr.Zero); } - + + popupPosition = new Point(); + popupSize = new Size(); } /// @@ -372,12 +406,30 @@ namespace CefSharp.OffScreen } } + /// + /// Gets the size of the popup. + /// + /// Size of the popup. + public Size GetPopupSize() + { + return popupSize; + } + + /// + /// Gets the popup position. + /// + /// The popup position. + public Point GetPopupPosition() + { + return popupPosition; + } + /// /// Create the underlying browser. The instance address, browser settings and request context will be used. /// /// Window handle if any, IntPtr.Zero is the default /// An instance of the underlying offscreen browser has already been created, this method can only be called once. - + public void CreateBrowser(IntPtr windowHandle) { if (browserCreated) @@ -422,14 +474,44 @@ namespace CefSharp.OffScreen /// The bitmap size is determined by the Size property set earlier. /// /// Bitmap. - public Bitmap ScreenshotOrNull() + public Bitmap ScreenshotOrNull(PopupBlending blend = PopupBlending.Separate) { lock (BitmapLock) { - return Bitmap == null ? null : new Bitmap(Bitmap); + if (blend == PopupBlending.Blend) + { + if (PopupOpened && Bitmap != null && Popup != null) + { + return MergeBackGroundPopupBitmaps(Bitmap, Popup); + } + else + { + return Bitmap == null ? null : new Bitmap(Bitmap); + } + } + else + { + if (PopupOpened) + { + return Popup == null ? null : new Bitmap(Popup); + } + else + { + return Bitmap == null ? null : new Bitmap(Bitmap); + } + } } } + /// + /// Sets the Bitmap to render with a copy of bitmap in parameter. + /// + /// The bitmap which will be copied + protected void SetBitmap(Bitmap bitmap) + { + Bitmap = (Bitmap)bitmap.Clone(); + } + /// /// 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, @@ -617,15 +699,29 @@ namespace CefSharp.OffScreen public virtual void InvokeRenderAsync(BitmapInfo bitmapInfo) { var gdiBitmapInfo = (GdiBitmapInfo)bitmapInfo; - if (bitmapInfo.CreateNewBitmap) - { - if (Bitmap != null) - { - Bitmap.Dispose(); - Bitmap = null; - } - Bitmap = gdiBitmapInfo.CreateBitmap(); + if (bitmapInfo.CreateNewBitmap || PopupJustOpened) + { + if (!gdiBitmapInfo.IsPopup) + { + if (Bitmap != null) + { + Bitmap.Dispose(); + Bitmap = null; + } + + Bitmap = gdiBitmapInfo.CreateBitmap(); + } + else + { + if (Popup != null) + { + Popup.Dispose(); + Popup = null; + } + Popup = gdiBitmapInfo.CreateBitmap(); + PopupJustOpened = false; + } } var handler = NewScreenshot; @@ -663,6 +759,21 @@ namespace CefSharp.OffScreen /// if set to true [show]. void IRenderWebBrowser.SetPopupIsOpen(bool show) { + if (show) + { + PopupOpened = true; + PopupJustOpened = true; + } + else + { + PopupOpened = false; + PopupJustOpened = false; + if (Popup != null) + { + Popup.Dispose(); + Popup = null; + } + } } /// @@ -674,6 +785,10 @@ namespace CefSharp.OffScreen /// The y. void IRenderWebBrowser.SetPopupSizeAndPosition(int width, int height, int x, int y) { + popupPosition.X = x; + popupPosition.Y = y; + popupSize.Width = width; + popupSize.Height = height; } /// @@ -751,7 +866,7 @@ namespace CefSharp.OffScreen /// The browser adapter. IBrowserAdapter IWebBrowserInternal.BrowserAdapter { - get { return managedCefBrowserAdapter;} + get { return managedCefBrowserAdapter; } } /// @@ -826,5 +941,24 @@ namespace CefSharp.OffScreen { TooltipText = tooltipText; } + + /// + /// Merges the popup bitmap in the background bitmap. + /// + /// The merged bitmap, size of the background bitmap + private Bitmap MergeBackGroundPopupBitmaps(Bitmap background, Bitmap popup) + { + Bitmap myBitmap = new Bitmap(background.Width, + background.Height, PixelFormat.Format32bppArgb); + using (Graphics g = Graphics.FromImage(myBitmap)) + { + g.DrawImage(background, + new Rectangle(0, 0, background.Width, background.Height)); + + g.DrawImage(popup, + new Rectangle((int)popupPosition.X, (int)popupPosition.Y, popup.Width, popup.Height)); + } + return myBitmap; + } } } diff --git a/CefSharp.OffScreen/PopupBlending.cs b/CefSharp.OffScreen/PopupBlending.cs new file mode 100644 index 000000000..848783db1 --- /dev/null +++ b/CefSharp.OffScreen/PopupBlending.cs @@ -0,0 +1,21 @@ +// Copyright © 2010-2016 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +namespace CefSharp.OffScreen +{ + /// + /// Represents the popup blending in the background bitmap. + /// + public enum PopupBlending + { + /// + /// The background and popup should be blended. + /// + Blend, + /// + /// The background and popup should be kept separated. + /// + Separate + } +}