diff --git a/CefSharp.Core/Internals/RenderClientAdapter.h b/CefSharp.Core/Internals/RenderClientAdapter.h index f2c3796ad..7af4fe085 100644 --- a/CefSharp.Core/Internals/RenderClientAdapter.h +++ b/CefSharp.Core/Internals/RenderClientAdapter.h @@ -29,9 +29,6 @@ namespace CefSharp _webBrowserInternal(webBrowserInternal) { _renderWebBrowser = dynamic_cast(webBrowserInternal); - - _mainBitmapInfo = _renderWebBrowser->CreateBitmapInfo(false); - _popupBitmapInfo = _renderWebBrowser->CreateBitmapInfo(true); } ~RenderClientAdapter() @@ -50,14 +47,18 @@ namespace CefSharp _popupBitmapInfo = nullptr; } + void CreateBitmapInfo() + { + _mainBitmapInfo = _renderWebBrowser->CreateBitmapInfo(false); + _popupBitmapInfo = _renderWebBrowser->CreateBitmapInfo(true); + } + // CefClient virtual DECL CefRefPtr GetRenderHandler() OVERRIDE{ return this; }; // CefRenderHandler virtual DECL bool GetScreenInfo(CefRefPtr browser, CefScreenInfo& screen_info) OVERRIDE { - return false; - if ((IRenderWebBrowser^)_renderWebBrowser == nullptr) { return false; @@ -84,11 +85,10 @@ namespace CefSharp auto screenInfo = _renderWebBrowser->GetScreenInfo(); - //auto scaledWidth = screenInfo.Width / screenInfo.ScaleFactor; - //auto scaledHeight = screenInfo.Height / screenInfo.ScaleFactor; - //rect = CefRect(0, 0, scaledWidth, scaledHeight); + auto scaledWidth = screenInfo.Width; + auto scaledHeight = screenInfo.Height; + rect = CefRect(0, 0, scaledWidth, scaledHeight); - rect = CefRect(0, 0, screenInfo.Width, screenInfo.Height); return true; }; @@ -187,22 +187,25 @@ namespace CefSharp private: void ReleaseBitmapHandlers(BitmapInfo^ bitmapInfo) { - auto backBufferHandle = (HANDLE)bitmapInfo->BackBufferHandle; - auto fileMappingHandle = (HANDLE)bitmapInfo->FileMappingHandle; + if(bitmapInfo) + { + auto backBufferHandle = (HANDLE)bitmapInfo->BackBufferHandle; + auto fileMappingHandle = (HANDLE)bitmapInfo->FileMappingHandle; - if (backBufferHandle != NULL) - { - UnmapViewOfFile(backBufferHandle); - backBufferHandle = NULL; - bitmapInfo->BackBufferHandle = IntPtr::Zero; - } + if (backBufferHandle != NULL) + { + UnmapViewOfFile(backBufferHandle); + backBufferHandle = NULL; + bitmapInfo->BackBufferHandle = IntPtr::Zero; + } - if (fileMappingHandle != NULL) - { - CloseHandle(fileMappingHandle); - fileMappingHandle = NULL; - bitmapInfo->FileMappingHandle = IntPtr::Zero; - } + if (fileMappingHandle != NULL) + { + CloseHandle(fileMappingHandle); + fileMappingHandle = NULL; + bitmapInfo->FileMappingHandle = IntPtr::Zero; + } + } } IMPLEMENT_REFCOUNTING(RenderClientAdapter) diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.cpp b/CefSharp.Core/ManagedCefBrowserAdapter.cpp index 2f70c2a02..ae2c118e8 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.cpp +++ b/CefSharp.Core/ManagedCefBrowserAdapter.cpp @@ -16,6 +16,10 @@ bool ManagedCefBrowserAdapter::IsDisposed::get() void ManagedCefBrowserAdapter::CreateOffscreenBrowser(IntPtr windowHandle, BrowserSettings^ browserSettings, RequestContext^ requestContext, String^ address) { + //Create the required BitmapInfo classes before the offscreen browser is initialized + auto renderClientAdapter = dynamic_cast(_clientAdapter.get()); + renderClientAdapter->CreateBitmapInfo(); + auto hwnd = static_cast(windowHandle.ToPointer()); CefWindowInfo window; diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 839a90970..da6efd8f5 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -22,7 +22,6 @@ namespace CefSharp.Wpf public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrowser { private readonly List disposables = new List(); - private readonly ScaleTransform imageTransform; private HwndSource source; private HwndSourceHook sourceHook; @@ -144,7 +143,6 @@ namespace CefSharp.Wpf UndoCommand = new DelegateCommand(this.Undo); RedoCommand = new DelegateCommand(this.Redo); - imageTransform = new ScaleTransform(); managedCefBrowserAdapter = new ManagedCefBrowserAdapter(this, true); disposables.Add(managedCefBrowserAdapter); @@ -233,10 +231,8 @@ namespace CefSharp.Wpf { var screenInfo = new ScreenInfo(); - var point = matrix.Transform(new Point(ActualWidth, ActualHeight)); - - screenInfo.Width = (int)point.X; - screenInfo.Height = (int)point.Y; + screenInfo.Width = (int)ActualWidth; + screenInfo.Height = (int)ActualHeight; screenInfo.ScaleFactor = (float)matrix.M11; return screenInfo; @@ -248,7 +244,7 @@ namespace CefSharp.Wpf { throw new Exception("BitmapFactory cannot be null"); } - return BitmapFactory.CreateBitmap(isPopup); + return BitmapFactory.CreateBitmap(isPopup, matrix.M11); } bool IRenderWebBrowser.StartDragging(IDragData dragData, DragOperationsMask mask, int x, int y) @@ -814,13 +810,16 @@ namespace CefSharp.Wpf if (source != null) { + var notifyDpiChanged = !matrix.Equals(source.CompositionTarget.TransformToDevice); + matrix = source.CompositionTarget.TransformToDevice; sourceHook = SourceHook; source.AddHook(sourceHook); - managedCefBrowserAdapter.NotifyScreenInfoChanged(); - imageTransform.ScaleX = 1 / matrix.M11; - imageTransform.ScaleY = 1 / matrix.M22; + if (notifyDpiChanged) + { + managedCefBrowserAdapter.NotifyScreenInfoChanged(); + } } } else if (args.OldSource != null) @@ -910,8 +909,6 @@ namespace CefSharp.Wpf img.Stretch = Stretch.None; img.HorizontalAlignment = HorizontalAlignment.Left; img.VerticalAlignment = VerticalAlignment.Top; - //Scale Image based on DPI settings - img.LayoutTransform = imageTransform; return img; } @@ -985,10 +982,10 @@ namespace CefSharp.Wpf private void SetPopupSizeAndPositionImpl(int width, int height, int x, int y) { - popup.Width = width / matrix.M11; - popup.Height = height / matrix.M22; + popup.Width = width ; + popup.Height = height; - var popupOffset = new Point(x / matrix.M11, y / matrix.M22); + var popupOffset = new Point(x, y); var locationFromScreen = PointToScreen(popupOffset); popup.HorizontalOffset = locationFromScreen.X / matrix.M11; popup.VerticalOffset = locationFromScreen.Y / matrix.M22; @@ -1251,9 +1248,8 @@ namespace CefSharp.Wpf private Point GetPixelPosition(MouseEventArgs e) { var deviceIndependentPosition = e.GetPosition(this); - var pixelPosition = matrix.Transform(deviceIndependentPosition); - return pixelPosition; + return deviceIndependentPosition; } public IBrowser GetBrowser() diff --git a/CefSharp.Wpf/Rendering/BitmapFactory.cs b/CefSharp.Wpf/Rendering/BitmapFactory.cs index 9d5bc1ca2..1b0028cdb 100644 --- a/CefSharp.Wpf/Rendering/BitmapFactory.cs +++ b/CefSharp.Wpf/Rendering/BitmapFactory.cs @@ -8,10 +8,21 @@ namespace CefSharp.Wpf.Rendering { public class BitmapFactory : IBitmapFactory { - public BitmapInfo CreateBitmap(bool isPopup) + public const int DefaultDpi = 96; + + public BitmapInfo CreateBitmap(bool isPopup, double dpiScale) { - //return new WritableBitmapInfo { IsPopup = isPopup }; - return new InteropBitmapInfo { IsPopup = isPopup }; + if (dpiScale > 1.0) + { + return new WritableBitmapInfo(DefaultDpi * dpiScale, DefaultDpi * dpiScale) + { + IsPopup = isPopup + }; + } + else + { + return new InteropBitmapInfo { IsPopup = isPopup }; + } } } } diff --git a/CefSharp.Wpf/Rendering/WritableBitmapInfo.cs b/CefSharp.Wpf/Rendering/WritableBitmapInfo.cs index 725fb70be..3f4bdacb5 100644 --- a/CefSharp.Wpf/Rendering/WritableBitmapInfo.cs +++ b/CefSharp.Wpf/Rendering/WritableBitmapInfo.cs @@ -14,9 +14,19 @@ namespace CefSharp.Wpf.Rendering private static readonly PixelFormat PixelFormat = PixelFormats.Bgra32; public WriteableBitmap Bitmap { get; private set; } + public double DpiX { get; private set; } + public double DpiY { get; private set; } public WritableBitmapInfo() + : this(BitmapFactory.DefaultDpi, BitmapFactory.DefaultDpi) { + } + + public WritableBitmapInfo(double dpiX, double dpiY) + { + DpiX = dpiX; + DpiY = dpiY; + BytesPerPixel = PixelFormat.BitsPerPixel / 8; DirtyRectSupport = true; } @@ -48,7 +58,7 @@ namespace CefSharp.Wpf.Rendering public override BitmapSource CreateBitmap() { - Bitmap = new WriteableBitmap(Width, Height, 96, 96, PixelFormat, null); + Bitmap = new WriteableBitmap(Width, Height, DpiX, DpiY, PixelFormat, null); return Bitmap; } diff --git a/CefSharp/IBitmapFactory.cs b/CefSharp/IBitmapFactory.cs index a9bae1943..8caee01ac 100644 --- a/CefSharp/IBitmapFactory.cs +++ b/CefSharp/IBitmapFactory.cs @@ -8,6 +8,6 @@ namespace CefSharp { public interface IBitmapFactory { - BitmapInfo CreateBitmap(bool isPopup); + BitmapInfo CreateBitmap(bool isPopup, double dpiScale); } }