From 446eda343594c81e4811ff326c2a10d30d5d4c8f Mon Sep 17 00:00:00 2001 From: jamespearce2006 Date: Tue, 5 May 2015 18:05:44 +0100 Subject: [PATCH 1/3] WPF - Handle case where Bitmap is IntPtr.Zero Because the OnPaint arrives on a thread other than the UI thread, it the bitmap rendering process can occur after Dispose() has been called. --- CefSharp.Wpf/Rendering/InteropBitmapInfo.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs b/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs index d0571c64a7..a3982a66e4 100644 --- a/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs +++ b/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs @@ -2,6 +2,7 @@ // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. +using System; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; @@ -31,14 +32,24 @@ public override void ClearBitmap() public override void Invalidate() { - Bitmap.Invalidate(); + if (Bitmap != null) + { + Bitmap.Invalidate(); + } } public override BitmapSource CreateBitmap() { var stride = Width * BytesPerPixel; - Bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(FileMappingHandle, Width, Height, PixelFormat, stride, 0); + if (FileMappingHandle != IntPtr.Zero) + { + Bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(FileMappingHandle, Width, Height, PixelFormat, stride, 0); + } + else + { + ClearBitmap(); + } return Bitmap; } From 6c16aa2725ed577552652d62e1cd366dac513552 Mon Sep 17 00:00:00 2001 From: jamespearce2006 Date: Tue, 5 May 2015 22:42:13 +0100 Subject: [PATCH 2/3] switch to evaluate == first --- CefSharp.Wpf/Rendering/InteropBitmapInfo.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs b/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs index a3982a66e4..12d10053c3 100644 --- a/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs +++ b/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs @@ -42,13 +42,13 @@ public override BitmapSource CreateBitmap() { var stride = Width * BytesPerPixel; - if (FileMappingHandle != IntPtr.Zero) + if (FileMappingHandle == IntPtr.Zero) { - Bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(FileMappingHandle, Width, Height, PixelFormat, stride, 0); + ClearBitmap(); } else { - ClearBitmap(); + Bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(FileMappingHandle, Width, Height, PixelFormat, stride, 0); } return Bitmap; From 6fa4b470f7300eba698c0a4a588136cec24f75f6 Mon Sep 17 00:00:00 2001 From: jamespearce2006 Date: Tue, 5 May 2015 22:57:50 +0100 Subject: [PATCH 3/3] Return null immediately if file handle is not valid --- CefSharp.Wpf/Rendering/InteropBitmapInfo.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs b/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs index 12d10053c3..5dc53a0b69 100644 --- a/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs +++ b/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs @@ -42,9 +42,10 @@ public override BitmapSource CreateBitmap() { var stride = Width * BytesPerPixel; + // Unable to create bitmap without valid File Handle (Most likely control is being disposed) if (FileMappingHandle == IntPtr.Zero) { - ClearBitmap(); + return null; } else {