Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Created December 2, 2019 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnLBevan/26b4a3d3e22f1624c85c13f87afde9e4 to your computer and use it in GitHub Desktop.
Save JohnLBevan/26b4a3d3e22f1624c85c13f87afde9e4 to your computer and use it in GitHub Desktop.
Create icon from an image file.
function ConvertTo-Icon {
[CmdletBinding(DefaultParameterSetName = 'BySuffix')]
Param (
# Specifies the source image filename
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$Path
,
# the name of the .ico file
[Parameter(Mandatory = $true, ParameterSetName = 'ByLiteralName')]
[string]$Destination
,
# what to append to the end of the source path to give our new filename
[Parameter(Mandatory = $false, ParameterSetName = 'BySuffix')]
[string]$Suffix = '.ico'
)
Process {
if ($PSCmdlet.ParameterSetName -eq 'BySuffix') {
$Destination = '{0}{1}' -f $Path, $Suffix
}
try {
$outstream = [System.IO.File]::OpenWrite($Destination)
$sourceImage = [System.Drawing.Bitmap]([System.Drawing.Image]::FromFile($Path))
[System.Drawing.Icon]::FromHandle($sourceImage.GetHicon()).Save($outstream)
(New-Object -TypeName 'PSObject' -Property @{SourceImage = $Path; IconPath = $Destination})
} finally {
$outstream.Dispose()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment