Skip to content

Commit

Permalink
Fix transparency in capturePage (#12561)
Browse files Browse the repository at this point in the history
* Fix transparent window capture. Transparency is preserved and not converted to black pixels anymore

* Add test to make sure aplha channel exists in captured image
  • Loading branch information
nitsakh authored and zcbenz committed Apr 10, 2018
1 parent eca0436 commit 6fc819d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ content::ServiceWorkerContext* GetServiceWorkerContext(
void OnCapturePageDone(const base::Callback<void(const gfx::Image&)>& callback,
const SkBitmap& bitmap,
content::ReadbackResponse response) {
// Hack to enable transparency in captured image
// TODO(nitsakh) Remove hack once fixed in chromium
const_cast<SkBitmap&>(bitmap).setAlphaType(kPremul_SkAlphaType);
callback.Run(gfx::Image::CreateFrom1xBitmap(bitmap));
}

Expand Down
23 changes: 23 additions & 0 deletions spec/api-browser-window-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,<html><body background-color: rgba(255,255,255,0)></body></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)', () => {
Expand Down

0 comments on commit 6fc819d

Please sign in to comment.