Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Chad Miller

Chad Miller is a Senior Manager of Database Administration at Raymond James Financial. Chad has worked with Microsoft SQL Server since 1999 and has been automating administration tasks using Windows Powershell since 2007. Chad is the Project Coordinator/Developer of the Powershell-based Codeplex project SQL Server PowerShell Extensions (SQLPSX). Chad leads the Tampa Powershell User Group and is a frequent speaker at users groups, SQL Saturdays and Code Camps.

Creating Icons Files

While working on a PowerPack for PowerGUI I needed to create a bunch of icon files from bitmaps files so I started with a quick web search. I didn’t find any PowerShell scripts suited to the task, but did find an excellent  C# WinForm by Haresh Ambaliya:

http://code.msdn.microsoft.com/Convert-Image-file-to-Icon-c927d9f7

Although the C# app is useful, it operates on a single file rather than whole bunch of files, so I quickly turned out a PowerShell called ConvertTo-Icon which I also posted on PoshCode:

function ConvertTo-Icon
{
    [cmdletbinding()]
    param([Parameter(Mandatory=$true, ValueFromPipeline = $true)] $Path)
 
    process{
        if ($Path -is [string])
        { $Path = get-childitem $Path }
 
        $Path | foreach {
            $image = [System.Drawing.Image]::FromFile($($_.FullName))
 
            $FilePath =  "{0}\{1}.ico" -f $($_.DirectoryName), $($_.BaseName)
            $stream = [System.IO.File]::OpenWrite($FilePath)
 
            $bitmap = new-object System.Drawing.Bitmap $image
            $bitmap.SetResolution(72,72)
            $icon = [System.Drawing.Icon]::FromHandle($bitmap.GetHicon())
            $icon.Save($stream)
            $stream.Close()
        }
    }
 
 }

Using the Convertto-Icon  function against my directory of bitmap files I was to create my icon files:

PS D:\Icons> Get-ChildItem *.bmp | ConvertTo-Icon

This is often the pattern I follow when I need to create a script, first look to see if anyone else has already done it and if not look for C# examples which easily be translated into Powershell.

Comments

No comments.

Leave a Comment

Please register or log in to leave a comment.