winutil/functions/private/ConvertTo-Icon.ps1

94 lines
3.8 KiB
PowerShell
Raw Permalink Normal View History

function ConvertTo-Icon {
Test 2023 12 19 (#1294) * Compile Winutil * Issue #1283, #1280 fixes, more (#1288) * Explorer Fix * Wifi, Explorer Crash, WinUtil Icon fixes. First attempt at white theme * White theme * Fix for clashing microwin directories if process fails, now new directory will be generated * * Tested latest Windows 10 (22H2) images work fine * Made dialog box more clear for issue #1283 * Added better logic for handling takeown /D flag for different locals issue #1280 * Refreshed the UI to more modern look * Improved white theme * Regrouped Tweak tab to make more sense * Advanced tweaks were in a separate column but the button applied both Essential and advanced now they are in the same column and button applies both * All instant action buttons were moved to Customize preferences column * Explorer lockup Fix * Wifi, Explorer Crash, WinUtil Icon fixes. * Fix for clashing microwin directories if process fails, now new directory will be generated * Merge all * Theme improvement, adding icon to the shortcut * Ability to download oscdimg from github, reorginizing Apps to fit better on more (smaller screens) * Fixing release branch to WinUtil * Adding double click to fullscreen * Update Get-Oscdimg.ps1 --------- Co-authored-by: KonTy <KonTy@github.com> Co-authored-by: Chris Titus <contact@christitus.com> * Update winutil.ps1 * remove merc and thorium * Ashlyn Programs * Also inject drivers into boot.wim * copy #1291 new branch --------- Co-authored-by: ChrisTitusTech <ChrisTitusTech@users.noreply.github.com> Co-authored-by: KonTy <9524513+KonTy@users.noreply.github.com> Co-authored-by: KonTy <KonTy@github.com> Co-authored-by: Cedric Lewe <0skillallluck@pm.me>
2023-12-19 13:55:55 -06:00
<#
Test 2023 12 19 (#1294) * Compile Winutil * Issue #1283, #1280 fixes, more (#1288) * Explorer Fix * Wifi, Explorer Crash, WinUtil Icon fixes. First attempt at white theme * White theme * Fix for clashing microwin directories if process fails, now new directory will be generated * * Tested latest Windows 10 (22H2) images work fine * Made dialog box more clear for issue #1283 * Added better logic for handling takeown /D flag for different locals issue #1280 * Refreshed the UI to more modern look * Improved white theme * Regrouped Tweak tab to make more sense * Advanced tweaks were in a separate column but the button applied both Essential and advanced now they are in the same column and button applies both * All instant action buttons were moved to Customize preferences column * Explorer lockup Fix * Wifi, Explorer Crash, WinUtil Icon fixes. * Fix for clashing microwin directories if process fails, now new directory will be generated * Merge all * Theme improvement, adding icon to the shortcut * Ability to download oscdimg from github, reorginizing Apps to fit better on more (smaller screens) * Fixing release branch to WinUtil * Adding double click to fullscreen * Update Get-Oscdimg.ps1 --------- Co-authored-by: KonTy <KonTy@github.com> Co-authored-by: Chris Titus <contact@christitus.com> * Update winutil.ps1 * remove merc and thorium * Ashlyn Programs * Also inject drivers into boot.wim * copy #1291 new branch --------- Co-authored-by: ChrisTitusTech <ChrisTitusTech@users.noreply.github.com> Co-authored-by: KonTy <9524513+KonTy@users.noreply.github.com> Co-authored-by: KonTy <KonTy@github.com> Co-authored-by: Cedric Lewe <0skillallluck@pm.me>
2023-12-19 13:55:55 -06:00
.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.
Test 2023 12 19 (#1294) * Compile Winutil * Issue #1283, #1280 fixes, more (#1288) * Explorer Fix * Wifi, Explorer Crash, WinUtil Icon fixes. First attempt at white theme * White theme * Fix for clashing microwin directories if process fails, now new directory will be generated * * Tested latest Windows 10 (22H2) images work fine * Made dialog box more clear for issue #1283 * Added better logic for handling takeown /D flag for different locals issue #1280 * Refreshed the UI to more modern look * Improved white theme * Regrouped Tweak tab to make more sense * Advanced tweaks were in a separate column but the button applied both Essential and advanced now they are in the same column and button applies both * All instant action buttons were moved to Customize preferences column * Explorer lockup Fix * Wifi, Explorer Crash, WinUtil Icon fixes. * Fix for clashing microwin directories if process fails, now new directory will be generated * Merge all * Theme improvement, adding icon to the shortcut * Ability to download oscdimg from github, reorginizing Apps to fit better on more (smaller screens) * Fixing release branch to WinUtil * Adding double click to fullscreen * Update Get-Oscdimg.ps1 --------- Co-authored-by: KonTy <KonTy@github.com> Co-authored-by: Chris Titus <contact@christitus.com> * Update winutil.ps1 * remove merc and thorium * Ashlyn Programs * Also inject drivers into boot.wim * copy #1291 new branch --------- Co-authored-by: ChrisTitusTech <ChrisTitusTech@users.noreply.github.com> Co-authored-by: KonTy <9524513+KonTy@users.noreply.github.com> Co-authored-by: KonTy <KonTy@github.com> Co-authored-by: Cedric Lewe <0skillallluck@pm.me>
2023-12-19 13:55:55 -06:00
.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.
Test 2023 12 19 (#1294) * Compile Winutil * Issue #1283, #1280 fixes, more (#1288) * Explorer Fix * Wifi, Explorer Crash, WinUtil Icon fixes. First attempt at white theme * White theme * Fix for clashing microwin directories if process fails, now new directory will be generated * * Tested latest Windows 10 (22H2) images work fine * Made dialog box more clear for issue #1283 * Added better logic for handling takeown /D flag for different locals issue #1280 * Refreshed the UI to more modern look * Improved white theme * Regrouped Tweak tab to make more sense * Advanced tweaks were in a separate column but the button applied both Essential and advanced now they are in the same column and button applies both * All instant action buttons were moved to Customize preferences column * Explorer lockup Fix * Wifi, Explorer Crash, WinUtil Icon fixes. * Fix for clashing microwin directories if process fails, now new directory will be generated * Merge all * Theme improvement, adding icon to the shortcut * Ability to download oscdimg from github, reorginizing Apps to fit better on more (smaller screens) * Fixing release branch to WinUtil * Adding double click to fullscreen * Update Get-Oscdimg.ps1 --------- Co-authored-by: KonTy <KonTy@github.com> Co-authored-by: Chris Titus <contact@christitus.com> * Update winutil.ps1 * remove merc and thorium * Ashlyn Programs * Also inject drivers into boot.wim * copy #1291 new branch --------- Co-authored-by: ChrisTitusTech <ChrisTitusTech@users.noreply.github.com> Co-authored-by: KonTy <9524513+KonTy@users.noreply.github.com> Co-authored-by: KonTy <KonTy@github.com> Co-authored-by: Cedric Lewe <0skillallluck@pm.me>
2023-12-19 13:55:55 -06:00
#>
param(
[Parameter(Mandatory=$true, position=0)]
[string]$bitmapPath,
[Parameter(Mandatory=$true, 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'."
}
}