-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_icon.ps1
More file actions
60 lines (47 loc) · 1.62 KB
/
convert_icon.ps1
File metadata and controls
60 lines (47 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Add-Type -AssemblyName System.Drawing
$source = "icons/icon.png"
$dest = "icons/icon.ico"
if (-not (Test-Path $source)) {
Write-Error "Source file $source not found."
exit 1
}
$img = [System.Drawing.Image]::FromFile($source)
$width = 256
$height = 256
$resized = new-object System.Drawing.Bitmap $width, $height
$g = [System.Drawing.Graphics]::FromImage($resized)
$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$g.DrawImage($img, 0, 0, $width, $height)
# Save resized PNG to memory
$ms = new-object System.IO.MemoryStream
$resized.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
$pngBytes = $ms.ToArray()
$ms.Dispose()
# Write ICO container
$fs = [System.IO.File]::Create($dest)
# ICO Header (6 bytes)
$fs.WriteByte(0); $fs.WriteByte(0) # Reserved
$fs.WriteByte(1); $fs.WriteByte(0) # Type (1=Icon)
$fs.WriteByte(1); $fs.WriteByte(0) # Count (1 image)
# Directory Entry (16 bytes)
$fs.WriteByte(0) # Width (0=256)
$fs.WriteByte(0) # Height (0=256)
$fs.WriteByte(0) # ColorCount
$fs.WriteByte(0) # Reserved
$fs.WriteByte(1); $fs.WriteByte(0) # Planes (1)
$fs.WriteByte(32); $fs.WriteByte(0) # BPP (32)
# Image Size (4 bytes)
$len = $pngBytes.Length
$fs.WriteByte([byte]($len -band 0xFF))
$fs.WriteByte([byte](($len -shr 8) -band 0xFF))
$fs.WriteByte([byte](($len -shr 16) -band 0xFF))
$fs.WriteByte([byte](($len -shr 24) -band 0xFF))
# Offset (4 bytes) -> 6 + 16 = 22
$fs.WriteByte(22); $fs.WriteByte(0); $fs.WriteByte(0); $fs.WriteByte(0)
# Image Data
$fs.Write($pngBytes, 0, $len)
$fs.Close()
$g.Dispose()
$resized.Dispose()
$img.Dispose()
Write-Host "Successfully created $dest from $source"