Created
December 2, 2019 10:54
-
-
Save JohnLBevan/26b4a3d3e22f1624c85c13f87afde9e4 to your computer and use it in GitHub Desktop.
Create icon from an image file.
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
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