Converting Bitmaps to Icons

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

If you need a new icon and have no icon editor at hand, then you can take a bitmap file (create one with MS Paint if you must) and have PowerShell convert it into an icon file.

The function code also illustrates how you can open an Explorer window and select a file inside of it.

function ConvertTo-Icon {
  param(
    [Parameter(Mandatory=$true)]
    $bitmapPath,
    $iconPath = "$env:temp\newicon.ico"
  )

Add-Type -AssemblyName System.Drawing

if (Test-Path $bitmapPath) {
    $b = [System.Drawing.Bitmap]::FromFile($bitmapPath)
    $icon = [System.Drawing.Icon]::FromHandle($b.GetHicon())
    $file = New-Object System.IO.FileStream($iconPath, 'OpenOrCreate') 
    $icon.Save($file)
    $file.Close()
    $icon.Dispose()

    explorer "/SELECT,$iconpath"
} else { Write-Warning "$BitmapPath does not exist" }
}

Twitter This Tip! ReTweet this Tip!


Posted Apr 12 2012, 06:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.