Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7350232362 | |||
| 67447bc7f1 | |||
| e80e673a10 | |||
| 408126f177 | |||
| c4d98dd5e3 | |||
| 789c7b3750 | |||
| a854d8e3f7 | |||
| 18eac693e9 | |||
| ebe64363c6 |
@@ -0,0 +1,55 @@
|
|||||||
|
name: Bug report
|
||||||
|
description: Something doesn't work as expected
|
||||||
|
labels: ["bug"]
|
||||||
|
body:
|
||||||
|
- type: input
|
||||||
|
id: version
|
||||||
|
attributes:
|
||||||
|
label: Version
|
||||||
|
description: Visible in the startup banner.
|
||||||
|
placeholder: v1.0.0
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: steps
|
||||||
|
attributes:
|
||||||
|
label: Steps to reproduce
|
||||||
|
placeholder: |
|
||||||
|
1. ...
|
||||||
|
2. ...
|
||||||
|
3. ...
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: expected
|
||||||
|
attributes:
|
||||||
|
label: What you expected to happen
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: actual
|
||||||
|
attributes:
|
||||||
|
label: What actually happened
|
||||||
|
description: Paste any error output verbatim. Screenshots are welcome.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: input
|
||||||
|
id: os
|
||||||
|
attributes:
|
||||||
|
label: Windows version
|
||||||
|
placeholder: e.g. Windows 11 24H2
|
||||||
|
|
||||||
|
- type: input
|
||||||
|
id: adapter
|
||||||
|
attributes:
|
||||||
|
label: Adapter (if relevant)
|
||||||
|
placeholder: e.g. Realtek USB GbE Family Controller
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: extra
|
||||||
|
attributes:
|
||||||
|
label: Anything else?
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
blank_issues_enabled: false
|
||||||
|
contact_links:
|
||||||
|
- name: Security vulnerability
|
||||||
|
url: https://github.com/Engelgardt23/netswitch/security/advisories/new
|
||||||
|
about: Please report security issues privately via GitHub Security Advisories — not as a public issue.
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
name: Feature request
|
||||||
|
description: Suggest a new feature or an improvement
|
||||||
|
labels: ["enhancement"]
|
||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
id: motivation
|
||||||
|
attributes:
|
||||||
|
label: What's the use case?
|
||||||
|
description: What are you trying to do, and why is the current behavior not enough?
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: proposal
|
||||||
|
attributes:
|
||||||
|
label: Proposed solution
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: alternatives
|
||||||
|
attributes:
|
||||||
|
label: Alternatives considered
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
name: Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v*.*.*'
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: windows-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Resolve version from tag
|
||||||
|
id: ver
|
||||||
|
shell: bash
|
||||||
|
run: echo "version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
- name: Install ps2exe
|
||||||
|
shell: pwsh
|
||||||
|
run: Install-Module -Name ps2exe -Scope CurrentUser -Force -AllowClobber
|
||||||
|
|
||||||
|
- name: Build executable
|
||||||
|
shell: pwsh
|
||||||
|
run: |
|
||||||
|
Import-Module ps2exe
|
||||||
|
$ver = '${{ steps.ver.outputs.version }}'
|
||||||
|
Invoke-ps2exe -inputFile src/netswitch.ps1 -outputFile netswitch.exe `
|
||||||
|
-title "netswitch" -description "NIC IP/DHCP toggle - made by engelgardt" `
|
||||||
|
-company "engelgardt" -version "$ver.0" `
|
||||||
|
-iconFile assets/icon.ico `
|
||||||
|
-requireAdmin
|
||||||
|
|
||||||
|
- name: Package portable folder
|
||||||
|
shell: pwsh
|
||||||
|
run: |
|
||||||
|
$ver = '${{ steps.ver.outputs.version }}'
|
||||||
|
$folder = "netswitch-v$ver"
|
||||||
|
New-Item -ItemType Directory -Path $folder | Out-Null
|
||||||
|
Copy-Item netswitch.exe $folder/
|
||||||
|
@"
|
||||||
|
netswitch v$ver - portable edition
|
||||||
|
made by engelgardt
|
||||||
|
|
||||||
|
Quickly set a Windows network adapter to a static IP or back to DHCP.
|
||||||
|
|
||||||
|
USAGE
|
||||||
|
Double-click netswitch.exe.
|
||||||
|
Accept the UAC prompt.
|
||||||
|
Pick the NIC, then choose mode (Static / DHCP).
|
||||||
|
Press Enter to exit.
|
||||||
|
|
||||||
|
NOTES
|
||||||
|
- Nothing is installed. Delete the folder to remove.
|
||||||
|
- Requires Windows 10/11.
|
||||||
|
- PowerShell is bundled into the exe via ps2exe; nothing extra needed.
|
||||||
|
"@ | Out-File -FilePath "$folder/README.txt" -Encoding UTF8
|
||||||
|
Compress-Archive -Path $folder -DestinationPath "netswitch-portable-v$ver.zip"
|
||||||
|
|
||||||
|
- name: Generate SHA-256 checksum
|
||||||
|
shell: pwsh
|
||||||
|
run: |
|
||||||
|
$ver = '${{ steps.ver.outputs.version }}'
|
||||||
|
$zip = "netswitch-portable-v$ver.zip"
|
||||||
|
$hash = (Get-FileHash -Algorithm SHA256 $zip).Hash.ToLower()
|
||||||
|
"$hash $zip" | Out-File -FilePath "$zip.sha256" -Encoding ASCII -NoNewline
|
||||||
|
Get-Content "$zip.sha256"
|
||||||
|
|
||||||
|
- name: Create release
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
files: |
|
||||||
|
netswitch-portable-v${{ steps.ver.outputs.version }}.zip
|
||||||
|
netswitch-portable-v${{ steps.ver.outputs.version }}.zip.sha256
|
||||||
|
generate_release_notes: true
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
All notable changes to **netswitch** are documented in this file.
|
||||||
|
|
||||||
|
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [1.0.3] - 2026-05-17
|
||||||
|
### Changed
|
||||||
|
- Update check no longer interrupts startup with an interactive prompt. If a newer release is available, a quiet right-aligned `update available (vX.Y.Z)` hint is printed in dim grey directly under the banner — no key press required.
|
||||||
|
|
||||||
|
## [1.0.2] - 2026-05-17
|
||||||
|
### Fixed
|
||||||
|
- Suppress `netsh` output to avoid mojibake on non-UTF-8 consoles (Russian Windows printed `╤Г╨╢╨╡ ╨▓╨║╨╗╤О╤З╨╡╨╜╨╛...` because netsh emits in the OEM code page). Our own English status lines remain; the post-change `Get-NetIPAddress` block already prints Unicode.
|
||||||
|
|
||||||
|
## [1.0.1] - 2026-05-17
|
||||||
|
### Changed
|
||||||
|
- Dropped the `made by engelgardt` line from the startup banner — keep author credit in the README only.
|
||||||
|
### Added
|
||||||
|
- Embedded application icon in the exe (via ps2exe `-iconFile assets/icon.ico`).
|
||||||
|
|
||||||
|
## [1.0.0] - 2026-05-16
|
||||||
|
### Added
|
||||||
|
- First public release on GitHub.
|
||||||
|
- Portable `.exe` (~30 KB) built from PowerShell via ps2exe.
|
||||||
|
- Self-elevation through UAC.
|
||||||
|
- Physical wired NIC filter — Wi-Fi, VPN, virtual, Hyper-V, VMware, VirtualBox, TAP/TUN, WireGuard, OpenVPN, Tailscale, ZeroTier, Bluetooth, Loopback and WAN Miniport adapters are hidden from the picker.
|
||||||
|
- Two modes: **Static** (default `10.10.10.1/24`, optional gateway) and **DHCP**.
|
||||||
|
- Current IPv4 configuration is printed after the change.
|
||||||
|
- Auto-update check on startup: polls GitHub `/releases/latest` with a 3-second timeout and offers to open the download page if a newer version exists. Silent on offline / API errors.
|
||||||
|
- MIT licensed.
|
||||||
|
|
||||||
|
[Unreleased]: https://github.com/Engelgardt23/netswitch/compare/v1.0.3...HEAD
|
||||||
|
[1.0.3]: https://github.com/Engelgardt23/netswitch/compare/v1.0.2...v1.0.3
|
||||||
|
[1.0.2]: https://github.com/Engelgardt23/netswitch/compare/v1.0.1...v1.0.2
|
||||||
|
[1.0.1]: https://github.com/Engelgardt23/netswitch/compare/v1.0.0...v1.0.1
|
||||||
|
[1.0.0]: https://github.com/Engelgardt23/netswitch/releases/tag/v1.0.0
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
# Contributing
|
||||||
|
|
||||||
|
> Project layout, build, and release flow. **If you only want to use the tool — read [README](README.md) instead.**
|
||||||
|
|
||||||
|
## Repo layout
|
||||||
|
|
||||||
|
```
|
||||||
|
netswitch/
|
||||||
|
├── .github/
|
||||||
|
│ ├── workflows/release.yml ← CI: tag-driven build + GitHub Release
|
||||||
|
│ └── ISSUE_TEMPLATE/ ← bug / feature / security routing
|
||||||
|
├── src/
|
||||||
|
│ └── netswitch.ps1 ← the whole tool (~150 lines)
|
||||||
|
├── CHANGELOG.md ← Keep a Changelog format, newest first
|
||||||
|
├── CONTRIBUTING.md ← this file
|
||||||
|
├── LICENSE / README.md / SECURITY.md
|
||||||
|
└── .gitignore
|
||||||
|
```
|
||||||
|
|
||||||
|
## Run from source (no exe)
|
||||||
|
|
||||||
|
```
|
||||||
|
powershell -ExecutionPolicy Bypass -File src/netswitch.ps1
|
||||||
|
```
|
||||||
|
|
||||||
|
The script self-elevates through UAC if you launched it without admin.
|
||||||
|
|
||||||
|
## Build the portable .exe locally
|
||||||
|
|
||||||
|
```
|
||||||
|
Install-Module ps2exe -Scope CurrentUser
|
||||||
|
Invoke-ps2exe -inputFile src/netswitch.ps1 -outputFile netswitch.exe `
|
||||||
|
-title "netswitch" -description "NIC IP/DHCP toggle - made by engelgardt" `
|
||||||
|
-company "engelgardt" -version "1.0.0.0" `
|
||||||
|
-requireAdmin
|
||||||
|
```
|
||||||
|
|
||||||
|
## Cut a release
|
||||||
|
|
||||||
|
1. Update `src/netswitch.ps1` — bump `$NetswitchVersion` to `X.Y.Z`.
|
||||||
|
2. Update `CHANGELOG.md` — move items from `[Unreleased]` into a new `[X.Y.Z]` section with today's date.
|
||||||
|
3. Commit: `git commit -am "vX.Y.Z: …"`.
|
||||||
|
4. Tag: `git tag vX.Y.Z`.
|
||||||
|
5. Push: `git push && git push --tags`.
|
||||||
|
|
||||||
|
GitHub Actions picks up the tag, builds the exe via ps2exe, writes the SHA-256, and creates the GitHub Release with the zip attached.
|
||||||
|
|
||||||
|
## Where features go
|
||||||
|
|
||||||
|
The whole tool is a single PowerShell file with these sections (in order):
|
||||||
|
|
||||||
|
1. `$NetswitchVersion` / `$GithubRepo` — top of file.
|
||||||
|
2. **Self-elevate** block — UAC if non-admin.
|
||||||
|
3. **Banner** — Write-Host title.
|
||||||
|
4. **`Test-NetswitchUpdate`** — GitHub /releases/latest poll.
|
||||||
|
5. **Adapter filter + picker** — `$skipDescriptionPattern`, `$skipMediaTypes`, `Get-NetAdapter | Where-Object {...}`.
|
||||||
|
6. **Mode prompt** — Static / DHCP.
|
||||||
|
7. **Static branch** — `netsh interface ipv4 set address ... static ...`.
|
||||||
|
8. **DHCP branch** — `netsh interface ipv4 set address ... source=dhcp`.
|
||||||
|
9. **Current config display** — `Get-NetIPAddress | Format-Table`.
|
||||||
|
|
||||||
|
If a section grows past ~50 lines, factor it into `src/lib/<thing>.ps1` and dot-source it from the main file.
|
||||||
+33
@@ -0,0 +1,33 @@
|
|||||||
|
# Security policy
|
||||||
|
|
||||||
|
Thanks for taking the time to look at this. Even small tools can introduce real
|
||||||
|
risk — this one reconfigures network adapters from an elevated process — so
|
||||||
|
vulnerability reports are very welcome.
|
||||||
|
|
||||||
|
## Supported versions
|
||||||
|
|
||||||
|
Only the latest tagged release on GitHub is supported. Older versions will not
|
||||||
|
get fixes; please upgrade first.
|
||||||
|
|
||||||
|
## How to report a vulnerability
|
||||||
|
|
||||||
|
**Please do not open a public issue** for security-sensitive findings.
|
||||||
|
|
||||||
|
Use GitHub's private security advisories: go to the
|
||||||
|
[Security tab](../../security/advisories/new) of this repo and click
|
||||||
|
"Report a vulnerability". GitHub will route it privately.
|
||||||
|
|
||||||
|
Please include:
|
||||||
|
- The version you tested (the startup banner is enough).
|
||||||
|
- Steps to reproduce.
|
||||||
|
- An assessment of impact.
|
||||||
|
|
||||||
|
Reports are reviewed and addressed on a best-effort basis. A fix and a public
|
||||||
|
advisory will be published once the issue is resolved. Reporters are credited
|
||||||
|
unless they prefer to stay anonymous.
|
||||||
|
|
||||||
|
## Out of scope
|
||||||
|
|
||||||
|
- Behavior when run **without** administrator privileges (the tool refuses to
|
||||||
|
start in that case anyway).
|
||||||
|
- Issues that require the attacker to already control the user's machine.
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 45 KiB |
@@ -1,7 +1,7 @@
|
|||||||
# netswitch v1.0.0 - quick NIC IP / DHCP toggle
|
# netswitch v1.0.0 - quick NIC IP / DHCP toggle
|
||||||
# made by engelgardt
|
# made by engelgardt
|
||||||
|
|
||||||
$NetswitchVersion = '1.0.0'
|
$NetswitchVersion = '1.0.3'
|
||||||
$GithubRepo = 'Engelgardt23/netswitch'
|
$GithubRepo = 'Engelgardt23/netswitch'
|
||||||
|
|
||||||
$ErrorActionPreference = 'Stop'
|
$ErrorActionPreference = 'Stop'
|
||||||
@@ -19,21 +19,13 @@ if (-not $me.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
|||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Banner ---
|
# --- Update check (silent: returns latest tag if newer, else empty) ---
|
||||||
Write-Host ""
|
function Get-NetswitchUpdate {
|
||||||
Write-Host "==============================================" -ForegroundColor Cyan
|
|
||||||
Write-Host " netswitch v$NetswitchVersion - NIC IP/DHCP toggle" -ForegroundColor Cyan
|
|
||||||
Write-Host " made by engelgardt" -ForegroundColor DarkCyan
|
|
||||||
Write-Host "==============================================" -ForegroundColor Cyan
|
|
||||||
Write-Host ""
|
|
||||||
|
|
||||||
# --- Update check ---
|
|
||||||
function Test-NetswitchUpdate {
|
|
||||||
try {
|
try {
|
||||||
$url = "https://api.github.com/repos/$GithubRepo/releases/latest"
|
$url = "https://api.github.com/repos/$GithubRepo/releases/latest"
|
||||||
$r = Invoke-RestMethod -Uri $url -TimeoutSec 3 -Headers @{ 'User-Agent' = "netswitch/$NetswitchVersion" }
|
$r = Invoke-RestMethod -Uri $url -TimeoutSec 3 -Headers @{ 'User-Agent' = "netswitch/$NetswitchVersion" }
|
||||||
$latest = ($r.tag_name -as [string]) -replace '^v',''
|
$latest = ($r.tag_name -as [string]) -replace '^v',''
|
||||||
if (-not $latest) { return }
|
if (-not $latest) { return '' }
|
||||||
$toTuple = { param($s)
|
$toTuple = { param($s)
|
||||||
$parts = ($s -split '\.') | ForEach-Object {
|
$parts = ($s -split '\.') | ForEach-Object {
|
||||||
$n = 0; [void][int]::TryParse($_, [ref]$n); $n
|
$n = 0; [void][int]::TryParse($_, [ref]$n); $n
|
||||||
@@ -43,24 +35,31 @@ function Test-NetswitchUpdate {
|
|||||||
}
|
}
|
||||||
$L = & $toTuple $latest
|
$L = & $toTuple $latest
|
||||||
$C = & $toTuple $NetswitchVersion
|
$C = & $toTuple $NetswitchVersion
|
||||||
$isNewer = $false
|
|
||||||
for ($i = 0; $i -lt 3; $i++) {
|
for ($i = 0; $i -lt 3; $i++) {
|
||||||
if ($L[$i] -gt $C[$i]) { $isNewer = $true; break }
|
if ($L[$i] -gt $C[$i]) { return $r.tag_name }
|
||||||
if ($L[$i] -lt $C[$i]) { break }
|
if ($L[$i] -lt $C[$i]) { return '' }
|
||||||
}
|
}
|
||||||
if ($isNewer) {
|
return ''
|
||||||
Write-Host "Update available: v$NetswitchVersion -> $($r.tag_name)" -ForegroundColor Yellow
|
} catch {
|
||||||
$ans = Read-Host "Open the download page in your browser? [Y/n]"
|
return ''
|
||||||
if ($ans -notmatch '^(n|N|no|NO)$') {
|
}
|
||||||
Start-Process $r.html_url
|
}
|
||||||
|
$latestTag = Get-NetswitchUpdate
|
||||||
|
|
||||||
|
# --- Banner ---
|
||||||
|
Write-Host ""
|
||||||
|
Write-Host "==============================================" -ForegroundColor Cyan
|
||||||
|
Write-Host " netswitch v$NetswitchVersion - NIC IP/DHCP toggle" -ForegroundColor Cyan
|
||||||
|
Write-Host "==============================================" -ForegroundColor Cyan
|
||||||
|
if ($latestTag) {
|
||||||
|
$msg = "update available ($latestTag)"
|
||||||
|
$w = 0
|
||||||
|
try { $w = $Host.UI.RawUI.WindowSize.Width } catch { $w = 0 }
|
||||||
|
if ($w -lt ($msg.Length + 2)) { $w = $msg.Length + 2 }
|
||||||
|
$pad = $w - $msg.Length - 1
|
||||||
|
Write-Host ((' ' * [Math]::Max(0, $pad)) + $msg) -ForegroundColor DarkGray
|
||||||
}
|
}
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
# silent on offline / API errors
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Test-NetswitchUpdate
|
|
||||||
|
|
||||||
# --- Pick adapter (physical wired only) ---
|
# --- Pick adapter (physical wired only) ---
|
||||||
$skipDescriptionPattern = 'VPN|Virtual|AnyConnect|TAP-|TUN-|Bluetooth|Loopback|WAN Miniport|Hyper-V|VMware|VirtualBox|WireGuard|OpenVPN|Tailscale|ZeroTier'
|
$skipDescriptionPattern = 'VPN|Virtual|AnyConnect|TAP-|TUN-|Bluetooth|Loopback|WAN Miniport|Hyper-V|VMware|VirtualBox|WireGuard|OpenVPN|Tailscale|ZeroTier'
|
||||||
@@ -108,8 +107,8 @@ if ($modeChoice.Trim() -eq '2') {
|
|||||||
# --- DHCP ---
|
# --- DHCP ---
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Host "Setting $($nic.Name) to DHCP..." -ForegroundColor Yellow
|
Write-Host "Setting $($nic.Name) to DHCP..." -ForegroundColor Yellow
|
||||||
& netsh interface ipv4 set address name="$($nic.Name)" source=dhcp
|
$null = & netsh interface ipv4 set address name="$($nic.Name)" source=dhcp 2>&1
|
||||||
& netsh interface ipv4 set dnsservers name="$($nic.Name)" source=dhcp
|
$null = & netsh interface ipv4 set dnsservers name="$($nic.Name)" source=dhcp 2>&1
|
||||||
Write-Host "Done." -ForegroundColor Green
|
Write-Host "Done." -ForegroundColor Green
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -126,9 +125,9 @@ else {
|
|||||||
Write-Host "Setting $($nic.Name) -> $ip / $mask$( if ($gw) { " via $gw" })" -ForegroundColor Yellow
|
Write-Host "Setting $($nic.Name) -> $ip / $mask$( if ($gw) { " via $gw" })" -ForegroundColor Yellow
|
||||||
|
|
||||||
if ([string]::IsNullOrWhiteSpace($gw)) {
|
if ([string]::IsNullOrWhiteSpace($gw)) {
|
||||||
& netsh interface ipv4 set address name="$($nic.Name)" static $ip $mask
|
$null = & netsh interface ipv4 set address name="$($nic.Name)" static $ip $mask 2>&1
|
||||||
} else {
|
} else {
|
||||||
& netsh interface ipv4 set address name="$($nic.Name)" static $ip $mask $gw
|
$null = & netsh interface ipv4 set address name="$($nic.Name)" static $ip $mask $gw 2>&1
|
||||||
}
|
}
|
||||||
Write-Host "Done." -ForegroundColor Green
|
Write-Host "Done." -ForegroundColor Green
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
# Regenerate assets/icon.ico for this project.
|
||||||
|
# Edit $Text / colors below and re-run.
|
||||||
|
|
||||||
|
param(
|
||||||
|
[string]$Text = "NET",
|
||||||
|
[int[]]$ColorFrom = @(120, 220, 140),
|
||||||
|
[int[]]$ColorTo = @(20, 110, 60),
|
||||||
|
[string]$OutPath = "$PSScriptRoot\..\assets\icon.ico"
|
||||||
|
)
|
||||||
|
|
||||||
|
Add-Type -AssemblyName System.Drawing
|
||||||
|
|
||||||
|
$outDir = Split-Path $OutPath -Parent
|
||||||
|
if (-not (Test-Path $outDir)) { New-Item -ItemType Directory -Path $outDir -Force | Out-Null }
|
||||||
|
|
||||||
|
$bgFrom = [System.Drawing.Color]::FromArgb(255, $ColorFrom[0], $ColorFrom[1], $ColorFrom[2])
|
||||||
|
$bgTo = [System.Drawing.Color]::FromArgb(255, $ColorTo[0], $ColorTo[1], $ColorTo[2])
|
||||||
|
|
||||||
|
$sizes = 256, 128, 64, 48, 32, 16
|
||||||
|
$pngs = @()
|
||||||
|
foreach ($s in $sizes) {
|
||||||
|
$bmp = New-Object System.Drawing.Bitmap $s, $s
|
||||||
|
$g = [System.Drawing.Graphics]::FromImage($bmp)
|
||||||
|
$g.SmoothingMode = 'AntiAlias'
|
||||||
|
$g.TextRenderingHint = 'AntiAliasGridFit'
|
||||||
|
|
||||||
|
$path = New-Object System.Drawing.Drawing2D.GraphicsPath
|
||||||
|
$r = $s - 2
|
||||||
|
$path.AddEllipse(1, 1, $r, $r)
|
||||||
|
$brush = New-Object System.Drawing.Drawing2D.PathGradientBrush($path)
|
||||||
|
$brush.CenterColor = $bgFrom
|
||||||
|
$brush.SurroundColors = @($bgTo)
|
||||||
|
$g.FillEllipse($brush, 1, 1, $r, $r)
|
||||||
|
|
||||||
|
$pen = New-Object System.Drawing.Pen ([System.Drawing.Color]::FromArgb(60, 0, 0, 0)), ([float]($s/64))
|
||||||
|
$g.DrawEllipse($pen, 1, 1, $r, $r)
|
||||||
|
|
||||||
|
$fontSize = [float]($s * 0.32)
|
||||||
|
$font = New-Object System.Drawing.Font "Segoe UI Black", $fontSize, ([System.Drawing.FontStyle]::Bold), ([System.Drawing.GraphicsUnit]::Pixel)
|
||||||
|
$sf = New-Object System.Drawing.StringFormat
|
||||||
|
$sf.Alignment = 'Center'; $sf.LineAlignment = 'Center'
|
||||||
|
$shadow = New-Object System.Drawing.SolidBrush ([System.Drawing.Color]::FromArgb(80, 0, 0, 0))
|
||||||
|
$g.DrawString($Text, $font, $shadow, [float]($s/2 + $s*0.02), [float]($s/2 + $s*0.02), $sf)
|
||||||
|
$g.DrawString($Text, $font, [System.Drawing.Brushes]::White, [float]($s/2), [float]($s/2), $sf)
|
||||||
|
$g.Dispose()
|
||||||
|
|
||||||
|
$ms = New-Object System.IO.MemoryStream
|
||||||
|
$bmp.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
|
||||||
|
$pngs += ,@{ size = $s; bytes = $ms.ToArray() }
|
||||||
|
$ms.Dispose(); $bmp.Dispose()
|
||||||
|
}
|
||||||
|
|
||||||
|
$fs = [System.IO.File]::Create($OutPath)
|
||||||
|
$bw = New-Object System.IO.BinaryWriter $fs
|
||||||
|
$bw.Write([uint16]0)
|
||||||
|
$bw.Write([uint16]1)
|
||||||
|
$bw.Write([uint16]$pngs.Count)
|
||||||
|
$offset = 6 + 16 * $pngs.Count
|
||||||
|
foreach ($p in $pngs) {
|
||||||
|
$w = if ($p.size -ge 256) { 0 } else { $p.size }
|
||||||
|
$bw.Write([byte]$w); $bw.Write([byte]$w)
|
||||||
|
$bw.Write([byte]0); $bw.Write([byte]0)
|
||||||
|
$bw.Write([uint16]1); $bw.Write([uint16]32)
|
||||||
|
$bw.Write([uint32]$p.bytes.Length); $bw.Write([uint32]$offset)
|
||||||
|
$offset += $p.bytes.Length
|
||||||
|
}
|
||||||
|
foreach ($p in $pngs) { $bw.Write($p.bytes) }
|
||||||
|
$bw.Close(); $fs.Close()
|
||||||
|
Write-Host "Wrote $OutPath"
|
||||||
Reference in New Issue
Block a user