-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-ico.ps1
More file actions
71 lines (64 loc) · 3.03 KB
/
create-ico.ps1
File metadata and controls
71 lines (64 loc) · 3.03 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
61
62
63
64
65
66
67
68
69
70
71
# Simple PNG to ICO converter using Windows API
Add-Type -TypeDefinition @"
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
public class IconConverter {
public static void ConvertPngToIco(string pngPath, string icoPath) {
using (var img = Image.FromFile(pngPath)) {
using (var ms = new MemoryStream()) {
// Create bitmap with proper format
using (var bmp = new Bitmap(img, 256, 256)) {
// Write ICO header
ms.WriteByte(0); // Reserved
ms.WriteByte(0); // Reserved
ms.WriteByte(1); // Type (1 = icon)
ms.WriteByte(0); // Type high byte
ms.WriteByte(1); // Number of images
ms.WriteByte(0); // Number high byte
// Write directory entry
ms.WriteByte(0); // Width (0 = 256)
ms.WriteByte(0); // Height (0 = 256)
ms.WriteByte(0); // Colors
ms.WriteByte(0); // Reserved
ms.WriteByte(1); // Planes
ms.WriteByte(0); // Planes high
ms.WriteByte(32); // Bits per pixel
ms.WriteByte(0); // Bits high
// Placeholder for size and offset
long sizePos = ms.Position;
ms.Write(new byte[8], 0, 8);
// Write PNG data
long dataStart = ms.Position;
using (var pngStream = new MemoryStream()) {
bmp.Save(pngStream, ImageFormat.Png);
var pngData = pngStream.ToArray();
ms.Write(pngData, 0, pngData.Length);
}
// Update size and offset
long dataEnd = ms.Position;
ms.Seek(sizePos, SeekOrigin.Begin);
var size = (int)(dataEnd - dataStart);
ms.Write(BitConverter.GetBytes(size), 0, 4);
ms.Write(BitConverter.GetBytes((int)dataStart), 0, 4);
// Write to file
File.WriteAllBytes(icoPath, ms.ToArray());
}
}
}
}
}
"@ -ReferencedAssemblies System.Drawing
try {
[IconConverter]::ConvertPngToIco("build\icon.png", "build\icon.ico")
Write-Host "Successfully created ICO file"
} catch {
Write-Error "Failed to create ICO: $($_.Exception.Message)"
# Fallback - create a minimal valid ICO header with PNG data
$pngBytes = [System.IO.File]::ReadAllBytes("build\icon.png")
$header = @(0,0,1,0,1,0,0,0,0,0,1,0,32,0) + [BitConverter]::GetBytes($pngBytes.Length) + [BitConverter]::GetBytes(22)
$icoBytes = $header + $pngBytes
[System.IO.File]::WriteAllBytes("build\icon.ico", $icoBytes)
Write-Host "Created ICO with PNG fallback method"
}