Last active
October 29, 2023 14:39
-
-
Save jbasinger/a0576a338b4c686a5318 to your computer and use it in GitHub Desktop.
Icon Extraction in Windows using NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Threading; | |
using System.Drawing; | |
using System.IO; | |
using Newtonsoft.Json; | |
namespace IconExtractor { | |
class IconRequest { | |
public string Context { get; set; } | |
public string Path { get; set; } | |
public string Base64ImageData { get; set; } | |
} | |
class Program { | |
static void Main(string[] args) { | |
// https://msdn.microsoft.com/en-us/library/ms404308%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 | |
Console.InputEncoding = UTF8Encoding.UTF8; | |
Console.OutputEncoding = UTF8Encoding.UTF8; | |
while (true) { | |
string input = Console.In.ReadLine().Trim(); | |
IconRequest data = JsonConvert.DeserializeObject<IconRequest>(input); | |
try { | |
data.Base64ImageData = getIconAsBase64(data.Path); | |
Console.WriteLine(JsonConvert.SerializeObject(data)); | |
} catch (Exception ex) { | |
Console.Error.WriteLine(ex); | |
Console.Error.WriteLine(input); | |
} | |
} | |
} | |
static string getIconAsBase64(string path) { | |
if (!File.Exists(path)) { | |
return ""; | |
} | |
Icon iconForPath = SystemIcons.WinLogo; | |
iconForPath = Icon.ExtractAssociatedIcon(path); | |
ImageConverter vert = new ImageConverter(); | |
byte[] data = (byte[])vert.ConvertTo(iconForPath.ToBitmap(), typeof(byte[])); | |
return Convert.ToBase64String(data); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EventEmitter = require('events'); | |
var fs = require('fs'); | |
var child_process = require('child_process'); | |
var _ = require('lodash'); | |
var os = require('os'); | |
var path = require('path'); | |
function IconExtractor(){ | |
var self = this; | |
var iconDataBuffer = ""; | |
this.emitter = new EventEmitter(); | |
this.iconProcess = child_process.spawn(getPlatformIconProcess()); | |
this.getIcon = function(context, path){ | |
var json = JSON.stringify({context: context, path: path}) + "\n"; | |
self.iconProcess.stdin.write(json); | |
} | |
this.iconProcess.stdout.on('data', function(data){ | |
var str = (new Buffer(data, 'utf8')).toString('utf8'); | |
iconDataBuffer += str; | |
//Bail if we don't have a complete string to parse yet. | |
if (!_.endsWith(str, '\n')){ | |
return; | |
} | |
//We might get more than one in the return, so we need to split that too. | |
_.each(iconDataBuffer.split('\n'), function(buf){ | |
if(!buf || buf.length == 0){ | |
return; | |
} | |
try{ | |
self.emitter.emit('icon', JSON.parse(buf)); | |
} catch(ex){ | |
self.emitter.emit('error', ex); | |
} | |
}); | |
}); | |
this.iconProcess.on('error', function(err){ | |
self.emitter.emit('error', err.toString()); | |
}); | |
this.iconProcess.stderr.on('data', function(err){ | |
self.emitter.emit('error', err.toString()); | |
}); | |
function getPlatformIconProcess(){ | |
if(os.type() == 'Windows_NT'){ | |
return path.join(__dirname,'/bin/IconExtractor.exe'); | |
//Do stuff here to get the icon that doesn't have the shortcut thing on it | |
} else { | |
throw('This platform (' + os.type() + ') is unsupported =('); | |
} | |
} | |
} | |
module.exports = new IconExtractor(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment