您好我正在使用电子截取整个屏幕的截图,我的代码运行良好,但下载屏幕截图的尺寸太小这一事实除外。
这是js函数,它点击按钮上的屏幕截图
renderer.js:
const electron = require('electron');
const desktopCapturer = electron.desktopCapturer;
const electronScreen = electron.screen;
const shell = electron.shell;
const remote = electron.remote;
const dialog = remote.dialog;
const fs = require('fs');
const os = require('os');
const path = require('path');
const screenshot = document.getElementById('screen-shot');
const screenshotMsg = document.getElementById('screenshot-path');
const pathButton = document.getElementById('path-button');
var screenShotPath ='';
pathButton.addEventListener('click',function (event) {
dialog.showSaveDialog(function (fileName) {
if(fileName == undefined){
return;
}
screenShotPath = fileName;
screenshotMsg.textContent = screenShotPath;
});
});
screenshot.addEventListener('click',function(event){
screenshotMsg.textContent = 'Gathering screens';
const thumbSize = determineScreenshot();
console.log(thumbSize.height);
console.log(thumbSize.width);
let options ={types: ['screen'], thumnailSize: thumbSize};
desktopCapturer.getSources(options,function (error,sources) {
console.log(sources);
if(error) return console.log(error.message);
sources.forEach(function (source) {
if(source.name === "Entire screen" || source.name === "Screen 1" ){
if(screenShotPath === ''){
screenShotPath = path.join(os.tmpdir(),'screenshot.jpeg');
}
console.log(screenShotPath);
fs.writeFile(screenShotPath,source.thumbnail.toPNG(), function (err) {
if(err) return console.log(err.message);
shell.openExternal("file://"+screenShotPath);
var message = 'Saved SS to ' + screenShotPath;
screenshotMsg.textContent = message;
});
}
});
});
});
function determineScreenshot() {
const screensize =electronScreen.getPrimaryDisplay().workAreaSize;
const maxDimension = Math.max(screensize.width,screensize.height);
console.log(maxDimension);
return {
width: maxDimension * window.devicePixelRatio,
height: maxDimension * window.devicePixelRatio
};
}