From 03cba68f6149b58d744f20b9b97b9bf5316b03de Mon Sep 17 00:00:00 2001 From: RKrom Date: Mon, 3 Dec 2012 13:16:23 +0000 Subject: [PATCH] This change enables a transparent background for those parts in the screen capture which are not visible on the screen. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2347 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- GreenshotPlugin/Core/WindowCapture.cs | 45 +++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/GreenshotPlugin/Core/WindowCapture.cs b/GreenshotPlugin/Core/WindowCapture.cs index a0b6840a5..33a205aef 100644 --- a/GreenshotPlugin/Core/WindowCapture.cs +++ b/GreenshotPlugin/Core/WindowCapture.cs @@ -701,10 +701,49 @@ namespace GreenshotPlugin.Core { // A suggestion for the "A generic error occurred in GDI+." E_FAIL/0×80004005 error is to re-try... bool success = false; ExternalException exception = null; - for(int i = 0; i < 3; i++) { + for (int i = 0; i < 3; i++) { try { - // assign image to Capture, the image will be disposed there.. - returnBitmap = Bitmap.FromHbitmap(hDIBSection); + // Collect all screens inside this capture + List screensInsideCapture = new List(); + foreach (Screen screen in Screen.AllScreens) { + if (screen.Bounds.IntersectsWith(captureBounds)) { + screensInsideCapture.Add(screen); + } + } + // Check all all screens are of an equal size + bool offscreenContent = false; + using (Region captureRegion = new Region(captureBounds)) { + // Exclude every visible part + foreach (Screen screen in screensInsideCapture) { + captureRegion.Exclude(screen.Bounds); + } + // If the region is not empty, we have "offscreenContent" + using (Graphics screenGraphics = Graphics.FromHwnd(User32.GetDesktopWindow())) { + offscreenContent = !captureRegion.IsEmpty(screenGraphics); + } + } + // Check if we need to have a transparent background, needed for offscreen content + if (offscreenContent) { + using (Bitmap tmpBitmap = Bitmap.FromHbitmap(hDIBSection)) { + // Create a new bitmap which has a transparent background + returnBitmap = ImageHelper.CreateEmpty(tmpBitmap.Width, tmpBitmap.Height, PixelFormat.Format32bppArgb, Color.Transparent, tmpBitmap.HorizontalResolution, tmpBitmap.VerticalResolution); + // Content will be copied here + using (Graphics graphics = Graphics.FromImage(returnBitmap)) { + // For all screens copy the content to the new bitmap + foreach (Screen screen in Screen.AllScreens) { + Rectangle screenBounds = screen.Bounds; + // Make sure the bounds are offsetted to the capture bounds + screenBounds.Offset(-captureBounds.X, -captureBounds.Y); + graphics.DrawImage(tmpBitmap, screenBounds, screenBounds.X, screenBounds.Y, screenBounds.Width, screenBounds.Height, GraphicsUnit.Pixel); + } + } + } + } else { + // All screens, which are inside the capture, are of equal size + // assign image to Capture, the image will be disposed there.. + returnBitmap = Bitmap.FromHbitmap(hDIBSection); + } + // We got through the capture without exception success = true; break; } catch (ExternalException ee) {