winutil/functions/private/ConvertTo-Icon.ps1
Mr.k 3903eaaa24
Code Formatting of Repo - Add Preprocessing to Compilation Process - Introduction of Dev/Build Tools to WinUtil (Although very simple at the moment) (#2383)
* Replace Tabs with Spaces to follow the conventions

* Add Preprocessing in Compiler

* Compile from Anywhere you want - Running 'Compile.ps1' Works in any directory you call it from

* Code Formatting Changes

* Result of Preprocessing Step in 'Compile.ps1' Script - Remove Trailing Whitespace Characters

* Make Preprocessing more advanced

* Move Preprocessing to a separate script file

* Make Self Modification impossible for 'tools/Do-PreProcessing.ps1' Script - Make the workingdir same as sync.PSScriptRoot for consistency

* Revert commit b5dffd671f

* Patched a Bug of some Excluded Files not actually get excluded in 'Get-ChildItem' PS Cmdlet

* Update Replace Regex for Code Formatting in 'Do-PreProcessing' Script Tool

* Rename 'Do-PreProcessing' to 'Invoke-Preprocessing' - Update some Comments

* Make 'Invoke-Preprocessing' Modular - Update RegEx to handle more cases - Update Documentation - Add Validations & Useful feedback upon error

* Replace Tabs with Spaces to follow the conventions - 'applications.json' File

* Code Formatting Changes - 'Copy-Files' Private Function

* Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool

* Replace Tabs with Spaces to follow the conventions - Make 'ExcludedFiles' validation step check all filepaths before finally checking if any has failed

* Result of 'Invoke-Preprocessing' Script

* Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool
2024-08-06 15:35:17 -05:00

93 lines
3.8 KiB
PowerShell

function ConvertTo-Icon {
<#
.DESCRIPTION
This function will convert BMP, GIF, EXIF, JPG, PNG and TIFF to ICO file
.PARAMETER bitmapPath
The file path to bitmap image to make '.ico' file out of.
Supported file types according to Microsoft Documentation is the following:
BMP, GIF, EXIF, JPG, PNG and TIFF.
.PARAMETER iconPath
The file path to write the new '.ico' resource.
.PARAMETER overrideIconFile
An optional boolean Parameter that makes the function overrides
the Icon File Path if the file exists. Defaults to $true.
.EXAMPLE
try {
ConvertTo-Icon -bitmapPath "$env:TEMP\cttlogo.png" -iconPath "$env:TEMP\cttlogo.ico"
} catch [System.IO.FileNotFoundException] {
# Handle the thrown exception here...
}
This Example makes a '.ico' file at "$env:TEMP\cttlogo.ico" File Path using the bitmap file
found in "$env:TEMP\cttlogo.png", the function overrides the '.ico' File if it's found.
this function will throw a FileNotFound Exception at the event of not finding the provided bitmap File Path.
.EXAMPLE
try {
ConvertTo-Icon "$env:TEMP\cttlogo.png" "$env:TEMP\cttlogo.ico"
} catch [System.IO.FileNotFoundException] {
# Handle the thrown exception here...
}
This Example is the same as Example 1, but uses Positional Parameters instead.
.EXAMPLE
if (Test-Path "$env:TEMP\cttlogo.png") {
ConvertTo-Icon -bitmapPath "$env:TEMP\cttlogo.png" -iconPath "$env:TEMP\cttlogo.ico"
}
This Example is same as Example 1, but checks if the bitmap File exists before calling 'ConvertTo-Icon' Function.
This's the recommended way of using this function, as it doesn't require any try-catch blocks.
.EXAMPLE
try {
ConvertTo-Icon -bitmapPath "$env:TEMP\cttlogo.png" -iconPath "$env:TEMP\cttlogo.ico" -overrideIconFile $false
} catch [System.IO.FileNotFoundException] {
# Handle the thrown exception here...
}
This Example make use of '-overrideIconFile' Optional Parameter, the default for this paramter is $true.
By doing '-overrideIconFile $false', the 'ConvertTo-Icon' function will raise an exception that needs to be catched throw a 'catch' Code Block,
otherwise it'll crash the running PowerShell instance/process.
#>
param(
[Parameter(Mandatory, position=0)]
[string]$bitmapPath,
[Parameter(Mandatory, position=1)]
[string]$iconPath,
[Parameter(position=2)]
[bool]$overrideIconFile = $true
)
Add-Type -AssemblyName System.Drawing
if (Test-Path $bitmapPath) {
if ((Test-Path $iconPath) -AND ($overrideIconFile -eq $false)) {
Write-Host "[ConvertTo-Icon] Icon File is found at '$iconPath', and the 'overrideIconFile' Parameter is set to '$overrideIconFile'. Skipping the bitmap to icon convertion..." -ForegroundColor Yellow
return
}
# Load bitmap file into memory, and make an Icon version out of it
$b = [System.Drawing.Bitmap]::FromFile($bitmapPath)
$icon = [System.Drawing.Icon]::FromHandle($b.GetHicon())
# Create the folder for the new icon file if it doesn't exists
$iconFolder = (New-Object System.IO.FileInfo($iconPath)).Directory.FullName
[System.IO.Directory]::CreateDirectory($iconFolder) | Out-Null
# Write the Icon File and do some cleaning-up
$file = New-Object System.IO.FileStream($iconPath, 'OpenOrCreate')
$icon.Save($file)
$file.Close()
$icon.Dispose()
} else {
throw [System.IO.FileNotFoundException] "[ConvertTo-Icon] The provided bitmap File Path is not found at '$bitmapPath'."
}
}