0

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?

3

1 Answer 1

1

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");
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.