diff --git a/CefSharp.Core/Internals/RenderClientAdapter.h b/CefSharp.Core/Internals/RenderClientAdapter.h index 620f25405e..1fd642eaf1 100644 --- a/CefSharp.Core/Internals/RenderClientAdapter.h +++ b/CefSharp.Core/Internals/RenderClientAdapter.h @@ -29,11 +29,10 @@ namespace CefSharp ClientAdapter(webBrowserInternal, onAfterBrowserCreated), _webBrowserInternal(webBrowserInternal) { - MainBitmapInfo = gcnew BitmapInfo(); - PopupBitmapInfo = gcnew BitmapInfo(); - PopupBitmapInfo->IsPopup = true; - _renderWebBrowser = dynamic_cast(webBrowserInternal); + + MainBitmapInfo = _renderWebBrowser->CreateBitmapInfo(false); + PopupBitmapInfo = _renderWebBrowser->CreateBitmapInfo(true); } ~RenderClientAdapter() @@ -184,8 +183,8 @@ namespace CefSharp bitmapInfo->Width != newWidth || bitmapInfo->Height != newHeight) { - //Clear the reference to InteropBitmap so a new one is created by InvokeRenderAsync - bitmapInfo->InteropBitmap = nullptr; + //Clear the reference to Bitmap so a new one is created by InvokeRenderAsync + bitmapInfo->ClearBitmap(); //Release the current handles (if not null) ReleaseBitmapHandlers(backBufferHandle, fileMappingHandle); diff --git a/CefSharp.OffScreen/CefSharp.OffScreen.csproj b/CefSharp.OffScreen/CefSharp.OffScreen.csproj index 9e5eac677a..7a2c28be10 100644 --- a/CefSharp.OffScreen/CefSharp.OffScreen.csproj +++ b/CefSharp.OffScreen/CefSharp.OffScreen.csproj @@ -64,6 +64,7 @@ + diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index a0a6b0a7fc..b245225561 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -363,6 +363,11 @@ int IRenderWebBrowser.Height get { return size.Height; } } + public BitmapInfo CreateBitmapInfo(bool isPopup) + { + return new GdiBitmapInfo { IsPopup = isPopup }; + } + void IRenderWebBrowser.InvokeRenderAsync(BitmapInfo bitmapInfo) { lock (bitmapLock) diff --git a/CefSharp.OffScreen/GdiBitmapInfo.cs b/CefSharp.OffScreen/GdiBitmapInfo.cs new file mode 100644 index 0000000000..968c72e6c5 --- /dev/null +++ b/CefSharp.OffScreen/GdiBitmapInfo.cs @@ -0,0 +1,15 @@ +using System.Drawing; +using CefSharp.Internals; + +namespace CefSharp.OffScreen +{ + public class GdiBitmapInfo : BitmapInfo + { + public Bitmap Bitmap { get; set; } + + public override void ClearBitmap() + { + Bitmap = null; + } + } +} diff --git a/CefSharp.Wpf/CefSharp.Wpf.csproj b/CefSharp.Wpf/CefSharp.Wpf.csproj index 3bfe48f298..0c7fa17114 100644 --- a/CefSharp.Wpf/CefSharp.Wpf.csproj +++ b/CefSharp.Wpf/CefSharp.Wpf.csproj @@ -93,6 +93,7 @@ + Code diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 24dc9db8e7..2cf66f1020 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -664,18 +664,24 @@ private IntPtr SourceHook(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam return IntPtr.Zero; } + public BitmapInfo CreateBitmapInfo(bool isPopup) + { + return new InteropBitmapInfo { IsPopup = isPopup }; + } + void IRenderWebBrowser.InvokeRenderAsync(BitmapInfo bitmapInfo) { UiThreadRunAsync(delegate { lock (bitmapInfo.BitmapLock) { + var interopBitmapInfo = (InteropBitmapInfo)bitmapInfo; // Inform parents that the browser rendering is updating - OnRendering(this, bitmapInfo); + OnRendering(this, interopBitmapInfo); var img = bitmapInfo.IsPopup ? popupImage : image; // Now update the WPF image - var bitmap = bitmapInfo.InteropBitmap as InteropBitmap; + var bitmap = interopBitmapInfo.InteropBitmap; if (bitmap == null) { img.Source = null; @@ -688,7 +694,7 @@ void IRenderWebBrowser.InvokeRenderAsync(BitmapInfo bitmapInfo) bitmapInfo.Width, bitmapInfo.Height, PixelFormat, stride, 0); img.Source = bitmap; - bitmapInfo.InteropBitmap = bitmap; + interopBitmapInfo.InteropBitmap = bitmap; } bitmap.Invalidate(); @@ -1247,7 +1253,7 @@ void IRenderWebBrowser.SetCursor(IntPtr handle) /// /// Raises Rendering event /// - protected virtual void OnRendering(object sender, BitmapInfo bitmapInfo) + protected virtual void OnRendering(object sender, InteropBitmapInfo bitmapInfo) { var rendering = Rendering; if (rendering != null) diff --git a/CefSharp.Wpf/InteropBitmapInfo.cs b/CefSharp.Wpf/InteropBitmapInfo.cs new file mode 100644 index 0000000000..20d0f7cab2 --- /dev/null +++ b/CefSharp.Wpf/InteropBitmapInfo.cs @@ -0,0 +1,16 @@ +using System.Windows.Interop; +using CefSharp.Internals; + +namespace CefSharp.Wpf +{ + public class InteropBitmapInfo : BitmapInfo + { + // Cannot be InteropBitmap since we really don't want CefSharp to be dependent on WPF libraries. + public InteropBitmap InteropBitmap { get; set; } + + public override void ClearBitmap() + { + InteropBitmap = null; + } + } +} diff --git a/CefSharp.Wpf/RenderingEventArgs.cs b/CefSharp.Wpf/RenderingEventArgs.cs index e0957aa0ca..d8d4e7ef3a 100644 --- a/CefSharp.Wpf/RenderingEventArgs.cs +++ b/CefSharp.Wpf/RenderingEventArgs.cs @@ -3,7 +3,6 @@ // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System; -using CefSharp.Internals; namespace CefSharp.Wpf { @@ -12,7 +11,7 @@ namespace CefSharp.Wpf /// public class RenderingEventArgs : EventArgs { - public RenderingEventArgs(BitmapInfo bitmapInfo) + public RenderingEventArgs(InteropBitmapInfo bitmapInfo) { BitmapInfo = bitmapInfo; } @@ -20,7 +19,7 @@ public RenderingEventArgs(BitmapInfo bitmapInfo) /// /// The bitmap info being rendered. /// - public BitmapInfo BitmapInfo { get; private set; } + public InteropBitmapInfo BitmapInfo { get; private set; } } } diff --git a/CefSharp/Internals/BitmapInfo.cs b/CefSharp/Internals/BitmapInfo.cs index a6b0a35017..4498b3ad87 100644 --- a/CefSharp/Internals/BitmapInfo.cs +++ b/CefSharp/Internals/BitmapInfo.cs @@ -6,7 +6,7 @@ namespace CefSharp.Internals { - public class BitmapInfo + public abstract class BitmapInfo { public object BitmapLock; public IntPtr BackBufferHandle; @@ -17,10 +17,9 @@ public class BitmapInfo public IntPtr FileMappingHandle { get; set; } - // Cannot be InteropBitmap since we really don't want CefSharp to be dependent on WPF libraries. - public object InteropBitmap; + public abstract void ClearBitmap(); - public BitmapInfo() + protected BitmapInfo() { BitmapLock = new object(); } diff --git a/CefSharp/Internals/IRenderWebBrowser.cs b/CefSharp/Internals/IRenderWebBrowser.cs index 223d3e93ff..354299b4c9 100644 --- a/CefSharp/Internals/IRenderWebBrowser.cs +++ b/CefSharp/Internals/IRenderWebBrowser.cs @@ -13,6 +13,7 @@ public interface IRenderWebBrowser : IWebBrowserInternal int Width { get; } int Height { get; } + BitmapInfo CreateBitmapInfo(bool isPopup); void InvokeRenderAsync(BitmapInfo bitmapInfo); void SetCursor(IntPtr cursor);