Scepticalist
Scepticalist

Reputation: 3923

How to extract icon using powershell - resulting file is not windows icon file

I have this simple script - which seems to be how every search I find tells me to extract an icon for use:

$Format = [System.Drawing.Imaging.ImageFormat]::Icon

$Source = 'C:\Windows\System32\DeviceProperties.exe'
[System.Drawing.Icon]::ExtractAssociatedIcon($Source).ToBitmap().Save("c:\temp\extracted.ico",$Format)

Problem being that the resulting file is not a Windows icon file. As evidenced when you try to apply this icon to a shortcut or similar I receive this Windows error:

The file C:\Temp\extracted contains no icons

Choose an icon from the list or specify a diferent file

I'm assuming here it's because the result is a bitmap rather than an icon format.

The question is - how to convert this to a proper icon file ready for use?

Upvotes: 4

Views: 7059

Answers (3)

Adelphos
Adelphos

Reputation: 103

The exported file: c:\temp\extracted.ico is a png file, not a ico file. I haven't found a way to convert this with powershell to ico format, either. But gimp e.g. can do this job.

Do the following:

  1. Open that file in gimp with (File / Open...)
  2. Click on Export as...
  3. Change file extension from .png to .ico (if necessary)
  4. Click on Export button
  5. Export Icon as Windows Icon window appears, click on Export button (or adjust settings, if necessary)

This will convert the file format from png to ico and you won't lose transparency.

Upvotes: 3

Joost
Joost

Reputation: 1216

I have encountered the same limitation a long time ago and gave up, I believe.

However, this one looks promising - a pre-made module to export, and it supposedly supports .ico format.

https://gallery.technet.microsoft.com/scriptcenter/Export-Icon-from-DLL-and-9d309047

I didn't test it, so I hope they didn't just rename the .bmp to .ico (because then it would probably not work either).

Upvotes: 2

Fabian
Fabian

Reputation: 459

An icon file is a file that contains many bitmap images (I think at some point they were 7). When trying to create an ico in GIMP for example you must put 7 bitmaps into it, each in it's own layer. I assume you should use something different than ToBitmap.

PS: I don't have access to a Windows machine to actually try your powershell script/command and I don't have enough reputation to just comment.

Upvotes: 0

Related Questions