2017-05-26 22 views
2

IM使用Web contents.capture()函數從窗口得到的截圖,但是圖像的質量不令人滿意時 繼承人的代碼電子低質量NativeImage toJPG

ipcRenderer.on('saved-file', (event, path) =>{ 
    if (path) { 
    remote.BrowserWindow.getFocusedWindow().webContents.[capturePage][1]((image)=>{ 
    scrImage = image.toJPEG(100); 
    console.log(scrImage.toString()); 
    fs.writeFile(path, scrImage, (err) => { 
     if (err) { 
     console.error(err); 
     } 
     console.log(path); 
    }); 

    }); 

sample image

,你可以看到在圖像上方Res對我們的用戶來說不夠好;電子Demo中的 DemoAPI質量的Capture圖像是一樣的。

有沒有其他方法可以獲得更好的質量?

+3

你能解釋一下對圖片不滿意嗎?發表示例 – HSchmale

+0

@Hschmale用戶聲稱它的用例不夠好 – alirezaac

+0

是否有toPng方法和resize方法? – HSchmale

回答

0

知道爲什麼質量不滿足您的用戶將是非常好的。它捕獲的圖像與您的BrowserWindow上的圖像完全相同。

但是NativeImage API爲您提供了一些方法來改善你的影像質量:

  • 可以轉換爲PNG
  • ,您可以調整圖像
  • 可以在高DPI diplays使用高DPI圖片

以下代碼PNG創建〜800 KB作爲截圖:

const { BrowserWindow, app } = require('electron') 
const fs = require('fs') 

app.once('ready',() => { 
    let win = new BrowserWindow({ 
    width: 1280, // win size affects captured image ofc 
    height: 720 
    }) 
    win.webContents.on('dom-ready',() => { 
    setTimeout(() => { 
     win.capturePage(img => { 
     var conv = img.resize({ // resize 
      width: 2560, 
      height: 1480, 
      quality: 'best' 
     }).toPNG(1.0) // to PNG 
     fs.writeFile(__dirname + '/captured.png', conv, err => { 
      if (err) console.log(err) 
     }); 
     }) 
    }, 3000); 
    }) 
    win.loadURL('http://github.com') 
}) 
+0

謝謝!它幫了很多,沒有足夠的代表upvot :) – alirezaac