-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIconGenerator.cs
More file actions
130 lines (115 loc) · 5.06 KB
/
IconGenerator.cs
File metadata and controls
130 lines (115 loc) · 5.06 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace ClippedImgToWSLPath
{
public static class IconGenerator
{
public static void GenerateIcon()
{
int size = 256;
using (Bitmap bitmap = new Bitmap(size, size))
using (Graphics g = Graphics.FromImage(bitmap))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.Clear(Color.Transparent);
// 背景の円
using (Brush bgBrush = new LinearGradientBrush(
new Point(0, 0), new Point(size, size),
Color.FromArgb(52, 152, 219), Color.FromArgb(41, 128, 185)))
{
g.FillEllipse(bgBrush, 10, 10, size - 20, size - 20);
}
// クリップボードアイコン
int clipWidth = size / 3;
int clipHeight = (int)(size / 2.5);
int clipX = (size - clipWidth) / 2;
int clipY = size / 4;
using (Pen pen = new Pen(Color.White, 8))
{
// クリップボード本体
Rectangle clipRect = new Rectangle(clipX, clipY, clipWidth, clipHeight);
g.FillRectangle(Brushes.White, clipRect);
// クリップ部分
int clipTopWidth = clipWidth / 3;
int clipTopHeight = size / 8;
Rectangle clipTop = new Rectangle(
clipX + (clipWidth - clipTopWidth) / 2,
clipY - clipTopHeight / 2,
clipTopWidth,
clipTopHeight);
g.FillRectangle(Brushes.White, clipTop);
}
// WSLテキスト
using (Font font = new Font("Arial", size / 8, FontStyle.Bold))
{
string text = "WSL";
SizeF textSize = g.MeasureString(text, font);
float textX = (size - textSize.Width) / 2;
float textY = size * 0.65f;
g.DrawString(text, font, Brushes.White, textX, textY);
}
// 複数サイズのアイコンを作成
CreateIconFile(bitmap, "icon.ico");
}
}
private static void CreateIconFile(Bitmap source, string fileName)
{
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
// ICONDIRヘッダー
fs.WriteByte(0); fs.WriteByte(0); // Reserved
fs.WriteByte(1); fs.WriteByte(0); // Type (1 = Icon)
fs.WriteByte(3); fs.WriteByte(0); // Count (3 images)
int offset = 6 + (16 * 3); // Header + 3 ICONDIRENTRY structures
// 各サイズのオフセットを計算
int[] sizes = { 16, 32, 48 };
int[] offsets = new int[sizes.Length];
offsets[0] = offset;
// 各サイズのバイト数を計算
for (int i = 1; i < sizes.Length; i++)
{
using (MemoryStream ms = new MemoryStream())
{
using (Bitmap resized = new Bitmap(source, sizes[i - 1], sizes[i - 1]))
{
resized.Save(ms, ImageFormat.Png);
offsets[i] = offsets[i - 1] + (int)ms.Length;
}
}
}
// ICONDIRENTRY structures
for (int i = 0; i < sizes.Length; i++)
{
fs.WriteByte((byte)sizes[i]); // Width
fs.WriteByte((byte)sizes[i]); // Height
fs.WriteByte(0); // Color palette
fs.WriteByte(0); // Reserved
fs.WriteByte(1); fs.WriteByte(0); // Color planes
fs.WriteByte(32); fs.WriteByte(0); // Bits per pixel
using (MemoryStream ms = new MemoryStream())
{
using (Bitmap resized = new Bitmap(source, sizes[i], sizes[i]))
{
resized.Save(ms, ImageFormat.Png);
byte[] sizeBytes = BitConverter.GetBytes((int)ms.Length);
fs.Write(sizeBytes, 0, 4); // Size
byte[] offsetBytes = BitConverter.GetBytes(offsets[i]);
fs.Write(offsetBytes, 0, 4); // Offset
}
}
}
// 実際の画像データ
for (int i = 0; i < sizes.Length; i++)
{
using (Bitmap resized = new Bitmap(source, sizes[i], sizes[i]))
{
resized.Save(fs, ImageFormat.Png);
}
}
}
}
}
}