I write default code from documentaion of Electron, but electron returns empty NativeImage object.
const { clipboard } = require("electron")
console.log(clipboard.readImage("clipboard"))
What should I do to paste image from clipboard?
For some reason, it seems that console.log
always display a NativeImage
empty object, whether there is an image in the clipboard or not.
Try to go a step further and convert the NativeImage
object to a data URL string, for instance; also, you should be able to check if the clipboard contains an image beforehand by using the clipboard.availableFormats
method.
The following code snippet works for me on macOS:
const { clipboard } = require("electron");
let availableFormats = clipboard.availableFormats("clipboard");
if (availableFormats.includes("image/png") || availableFormats.includes("image/jpeg"))
{
console.log(clipboard.readImage("clipboard").toDataURL());
}
else
{
console.log("No image in clipboard");
}
Currently PNG and JPEG image formats are supported
is this your case? electronjs.org/docs/api/native-image#nativeimage