Repository navigation
Expand file tree
/
Copy pathIconGenerator.cs
More file actions
85 lines (73 loc) · 2.99 KB
/
Copy pathIconGenerator.cs
File metadata and controls
85 lines (73 loc) · 2.99 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WindowsPrivacyTools
{
public static class IconGenerator
{
public static void GenerateIconIfMissing()
{
string iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wp.ico");
if (File.Exists(iconPath)) return;
try
{
var dv = new DrawingVisual();
using (var ctx = dv.RenderOpen())
{
// Background
ctx.DrawRectangle(
new SolidColorBrush(Color.FromRgb(0x18, 0x18, 0x25)),
null, new Rect(0, 0, 64, 64));
// Outer circle
ctx.DrawEllipse(
new SolidColorBrush(Color.FromRgb(0x89, 0xB4, 0xFA)),
null, new Point(32, 32), 28, 28);
// Inner dark circle
ctx.DrawEllipse(
new SolidColorBrush(Color.FromRgb(0x18, 0x18, 0x25)),
null, new Point(32, 32), 22, 22);
// WP text
var ft = new FormattedText("WP",
System.Globalization.CultureInfo.InvariantCulture,
FlowDirection.LeftToRight,
new Typeface(new FontFamily("Segoe UI"), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal),
20,
new SolidColorBrush(Color.FromRgb(0x89, 0xB4, 0xFA)),
96);
ft.TextAlignment = TextAlignment.Center;
ctx.DrawText(ft, new Point(32, 22));
}
var rtb = new RenderTargetBitmap(64, 64, 96, 96, PixelFormats.Pbgra32);
rtb.Render(dv);
string tempPng = Path.Combine(Path.GetTempPath(), "wp_icon_temp.png");
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
using (var fs = File.Create(tempPng))
encoder.Save(fs);
byte[] pngData = File.ReadAllBytes(tempPng);
CreateIcoFile(iconPath, pngData, 64, 64);
File.Delete(tempPng);
}
catch { }
}
private static void CreateIcoFile(string icoPath, byte[] pngData, int width, int height)
{
using var fs = File.Create(icoPath);
using var bw = new BinaryWriter(fs);
bw.Write((ushort)0);
bw.Write((ushort)1);
bw.Write((ushort)1);
bw.Write((byte)(width > 255 ? 0 : width));
bw.Write((byte)(height > 255 ? 0 : height));
bw.Write((byte)0);
bw.Write((byte)0);
bw.Write((ushort)1);
bw.Write((ushort)32);
bw.Write(pngData.Length);
bw.Write(6 + 16);
bw.Write(pngData);
}
}
}