0

I have an image that is stored in a buffer. I'm trying to set it as the window icon, but I can't find a way to do so. There is no path to the image, so I can't just use win.setIcon('path/to/image').

I tried to do the following, to no success.

win.setIcon(buffer); // giving the buffer by itself
win.setIcon(buffer.toString('base64')); // giving the buffer as base64
win.setIcon(`data:image/png;base64,${buffer.toString('base64')}`); // giving as base64 url

let imageObject = new Image();
imageObject.src = `data:image/png;base64,${buffer.toString('base64')}`;
win.setIcon(imageObject); // giving image object
hf02
  • 171
  • 2
  • 11
  • Does this answer your question? [How to set app icon for Electron / Atom Shell App](https://stackoverflow.com/questions/31529772/how-to-set-app-icon-for-electron-atom-shell-app) – Randy Casburn Nov 03 '20 at 01:39
  • @RandyCasburn, this is not a duplicate as OP's trying to set the image from a buffer and neither via a file nor via an app package. – Alexander Leithner Nov 03 '20 at 08:05

1 Answers1

0

According to Electron's documentation, BrowserWindow.setIcon () takes either a string or a NativeImage, a data type provided by Electron. You can convert your buffer to a NativeImage by using the following code:

const { nativeImage } = require ("electron");

win.setIcon (nativeImage.createFromBuffer (buffer));

If that does not help, you can also pass your buffer as a Base 64 string in a data URL (like you have tried before) to the function createFromDataURL. For more information, see the documentation on NativeImage. It is also worth noting that you can pass advanced options to the createFromBuffer function to give Electron more hints about how to display your icon.

Alexander Leithner
  • 3,169
  • 22
  • 33