electron: TypeError: Error processing argument at index 0, conversion failure from C:\Users\root\path\to\logo.ico

  • Electron version: 1.4.3
  • Operating system: Windows 10 under VirtualBox 5.0.26 r108824 running on OSX 10.11.6

Hi guys, I’m having trouble with an error that happens randomly at launch: I have an icon located at

const iconPath = "C:\Users\root\path\to\logo.ico"

And sometimes the line

const trayIcon = new Tray(iconPath)

throws the error

TypeError: Error processing argument at index 0, conversion failure from C:\Users\root\path\to\logo.ico.

Has anyone got an idea why this line sometimes throws an error and sometimes works properly, with the ico file being strictly the same ?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 38
  • Comments: 31 (5 by maintainers)

Commits related to this issue

Most upvoted comments

You can specify an absolute path. like this: image

Thanks @isNeilLin ,but it’s still not working correctly even if I’ve specified an absolute path. Do you have any other workaround?

If you are a Windows user, the problem had nothing to do with the image path but rather the setBounds values. The short of it is you must parseInt the values for the coordinates. This works for me:

  const {app, BrowserWindow, Menu, Tray} = require('electron');
  const {join} = require('path');

  let mainWindow, tray;

  app.on('ready', () => {
  mainWindow = new BrowserWindow({
    height: 500,
    width: 300,
    frame: false,
    resizable: false,
    // draggable: true,
    show: false
});
mainWindow.loadURL(`file://${__dirname}/src/index.html`);

const iconName = process.platform === 'win32' ? 'windows-icon@2x.png' : 'iconTemplate.png';
const iconPath = join(__dirname, `./src/assets/${iconName}`);
tray = new Tray(iconPath);
const contextMenu = Menu.buildFromTemplate([
    {label: 'Item1', type: 'radio'},
    {label: 'Item2', type: 'radio'},
    {label: 'Item3', type: 'radio', checked: true},
    {label: 'Item4', type: 'radio'}
]);
tray.setToolTip('Right Click Icon for Options.');
tray.setContextMenu(contextMenu);
tray.on('click', (ev, bounds) => {
    // Click event bounds
    const {x, y} = bounds;
    // Window height & width
    const {height, width} = mainWindow.getBounds();

    if (mainWindow.isVisible()) {
        mainWindow.hide();
    } else {

   /////////////////////////////////////////////////////////////////////////////////////////////
   // On Windows I had to parseInt the corrdinates
   /////////////////////////////////////////////////////////////////////////////////////////////
    const yPosition = process.platform === 'darwin' ? y : parseInt(y - height);
    const xPosition = process.platform === 'darwin' ? x - width / 2 : parseInt(x - (width/2));

        mainWindow.setBounds({
            x: xPosition,
            y: yPosition,
            height,
            width
        });
        mainWindow.show();
    }
})
  });

@littletinman You have to pass the TrayIcon a native image object. what you are passing is a STRING instead of nativeImage:

const trayIcon = path.join(__dirname, 'icons/icon-16x16.png');
const nimage = nativeImage.createFromPath(trayIcon);
const tray = new Tray(nimage);

In that way the trayIcon will work

In case someone ends up here without finding a solution that works, here are the steps I’d recommend:

  • are you passing a NativeImage value to Tray constructor? If not:
// If not already imported
// const path = require('path');
const iconPath = path.join(__dirname, 'tray-icon.png');
const trayIcon = nativeImage.createFromPath(iconPath);
const tray = new Tray(trayIcon);
  • does your icon follow any of the NativeImage supported formats? PNG and JPEG are the only two formats supported on all platforms.
  • remember that your icon should be sized 16*16px or respects NativeImage file sizes. Otherwise, resize it before:
let trayIcon = nativeImage.createFromPath(iconPath);
trayIcon = trayIcon.resize({ width: 16, height: 16 });

Why was this issue closed? This bug exists in Electron 8.2.3 (macosx), the latest version released just 4 days ago.

This error happens when the icon does not exist for any reason, throwing exception for this was a decision made on the first day and I’m still not sure whether it is a good design.

Anyway if you want to ignore this error you can use naitveImage to create an icon from path and then pass it to Tray, and it silences the error.

This issue is extremely annoying on both windows and macos. The icon files need to be crafted with magic spells, and randomly stop working once every couple electron updates. The image handling code should be re-written. For example, if you have 1024x1024 icon in .ico it wont work - WHY? Just discard the icon you dont need and not crash the entire application during startup. I know ico should not include icon of this size but this is an unnecessary bug that has been around forever.

@ue / @JoeGrasso Had the same problem and Math.floor fixed it:

const bounds = {
     x: Math.floor(x - width / 2),
     y: Math.floor(yPosition),
     height,
     width
 };

I can confirm that the issue was missing Math.floor and sending floating numbers to window

    window.setPosition(x, y);

replaced with

    window.setPosition(Math.floor(x), Math.floor(y));

We are calculating position to put 2 windows in center next to each other, and it could happen the numbers were floating point numbers and not whole. Crashes only on Windows. Rounding the numbers fixed the issue.

I’m having this issue on Windows 10 only after building the electron app for deployment.

Versions

electron 1.6.11
electron-packager 8.7.2

Code

const trayIcon = path.join(__dirname, 'icons/icon-16x16.png');
const tray = new Tray(trayIcon);

I was able to solve it figuring out if the file exists in the project files or not. for that I have first to verify the existence of the file, then convert the image as nativeImage. I’m using fs-jetpack to verify quickly if it is “file” the output of the console… The problem is that likely you guys are pointing to a file where the app doesn’t have access…

const {
	app,
	Tray,
	Menu,
	ipcMain,
	nativeImage
} = require('electron');
const main       = require('../main');
const path = require('path');
const jetpack = require('fs-jetpack');
let appIcon = null;

ipcMain.on('go-tray', (event) => {
	const iconName = '16.png';
	const iconPath = path.join(__dirname,'..','assets','icons',iconName);
	console.log(jetpack.exists(iconPath)); //should be "file", otherwise you are not pointing to your icon file 
	let nimage = nativeImage.createFromPath(iconPath);
	appIcon = new Tray(nimage);
	const contextMenu = Menu.buildFromTemplate([{
		label : 'Restore to Design Mode',
		click : () =>{
			appIcon.destroy();
			main.mainWindow.show();
		}
	}]);
	appIcon.setToolTip('app is on tray icon');
	appIcon.setContextMenu(contextMenu);
	main.mainWindow.hide();
});

app.on('window-all-closed',() => {
	if (appIcon) appIcon.destroy();
});