Files
xianren_studio/scripts/generate-icons.ps1
T

52 lines
2.1 KiB
PowerShell

param([string]$OutDir = "apps/desktop/icons")
$ErrorActionPreference = "Stop"
Add-Type -AssemblyName System.Drawing
$root = Split-Path $PSScriptRoot -Parent
$dir = Join-Path $root $OutDir
New-Item -ItemType Directory -Force -Path $dir | Out-Null
function New-IconPng([int]$size, [string]$path) {
$bmp = New-Object System.Drawing.Bitmap $size, $size
$g = [System.Drawing.Graphics]::FromImage($bmp)
$g.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
$rect = New-Object System.Drawing.Rectangle 0, 0, $size, $size
$brush = New-Object System.Drawing.Drawing2D.LinearGradientBrush $rect, `
([System.Drawing.Color]::FromArgb(255, 79, 70, 229)), `
([System.Drawing.Color]::FromArgb(255, 14, 165, 233)), 45
$g.FillRectangle($brush, $rect)
$font = New-Object System.Drawing.Font "Microsoft YaHei", ($size * 0.55), ([System.Drawing.FontStyle]::Bold)
$sf = New-Object System.Drawing.StringFormat
$sf.Alignment = [System.Drawing.StringAlignment]::Center
$sf.LineAlignment = [System.Drawing.StringAlignment]::Center
$rectF = New-Object System.Drawing.RectangleF 0, 0, $size, $size
$g.DrawString([string][char]0x4ED9, $font, [System.Drawing.Brushes]::White, $rectF, $sf)
$bmp.Save($path, [System.Drawing.Imaging.ImageFormat]::Png)
$g.Dispose(); $brush.Dispose(); $bmp.Dispose()
}
New-IconPng 32 (Join-Path $dir "32x32.png")
New-IconPng 128 (Join-Path $dir "128x128.png")
New-IconPng 256 (Join-Path $dir "128x128@2x.png")
$pngBytes = [System.IO.File]::ReadAllBytes((Join-Path $dir "128x128@2x.png"))
$fs = [System.IO.File]::Create((Join-Path $dir "icon.ico"))
$bw = New-Object System.IO.BinaryWriter $fs
$bw.Write([UInt16]0) # reserved
$bw.Write([UInt16]1) # type: icon
$bw.Write([UInt16]1) # count
$bw.Write([Byte]0) # width (256 -> 0)
$bw.Write([Byte]0) # height
$bw.Write([Byte]0) # palette
$bw.Write([Byte]0) # reserved
$bw.Write([UInt16]1) # color planes
$bw.Write([UInt16]32) # bpp
$bw.Write([UInt32]$pngBytes.Length)
$bw.Write([UInt32]22) # data offset
$bw.Write($pngBytes)
$bw.Close()
$fs.Close()
Write-Host "Icons written to $dir"