From b053129fd2251254ffd3222c37ab67e44141a07e Mon Sep 17 00:00:00 2001 From: Nitish Sakhawalkar Date: Fri, 6 Apr 2018 14:00:05 -0700 Subject: [PATCH 1/2] Fix transparent window capture. Transparency is preserved and not converted to black pixels anymore --- atom/browser/api/atom_api_web_contents.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index dd3e1e2499b75..1be932a0b558d 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -276,6 +276,9 @@ content::ServiceWorkerContext* GetServiceWorkerContext( void OnCapturePageDone(const base::Callback& callback, const SkBitmap& bitmap, content::ReadbackResponse response) { + // Hack to enable transparency in captured image + // TODO(nitsakh) Remove hack once fixed in chromium + const_cast(bitmap).setAlphaType(kPremul_SkAlphaType); callback.Run(gfx::Image::CreateFrom1xBitmap(bitmap)); } From 2ac2b9706bfa036dbfc559f9ec12982a6b37d768 Mon Sep 17 00:00:00 2001 From: Nitish Sakhawalkar Date: Fri, 6 Apr 2018 15:01:31 -0700 Subject: [PATCH 2/2] Add test to make sure aplha channel exists in captured image --- spec/api-browser-window-spec.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index d272203a874ba..4a3853fc0bb7f 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -442,6 +442,29 @@ describe('BrowserWindow module', () => { done() }) }) + + it('preserves transparency', (done) => { + w.close() + const width = 400 + const height = 400 + w = new BrowserWindow({ + show: false, + width: width, + height: height, + transparent: true + }) + w.loadURL('data:text/html,') + w.once('ready-to-show', () => { + w.show() + w.capturePage((image) => { + let imgBuffer = image.toPNG() + // Check 25th byte in the PNG + // Values can be 0,2,3,4, or 6. We want 6, which is RGB + Alpha + assert.equal(imgBuffer[25], 6) + done() + }) + }) + }) }) describe('BrowserWindow.setSize(width, height)', () => {