117 lines
3.8 KiB
C#
117 lines
3.8 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace TimerApp
|
|
{
|
|
public static class IconGenerator
|
|
{
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
private static extern bool DestroyIcon(IntPtr hIcon);
|
|
|
|
public static Icon GenerateClockIcon(int size = 64)
|
|
{
|
|
if (size < 16)
|
|
size = 16;
|
|
|
|
using (Bitmap bmp = new Bitmap(size, size))
|
|
using (Graphics g = Graphics.FromImage(bmp))
|
|
{
|
|
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
g.Clear(Color.Transparent);
|
|
|
|
DrawClock(g, size);
|
|
|
|
IntPtr hIcon = bmp.GetHicon();
|
|
try
|
|
{
|
|
using Icon icon = Icon.FromHandle(hIcon);
|
|
return (Icon)icon.Clone();
|
|
}
|
|
finally
|
|
{
|
|
DestroyIcon(hIcon);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void WriteClockIco(string filePath, int size = 256)
|
|
{
|
|
byte[] icoBytes = CreateClockIcoBytes(size);
|
|
File.WriteAllBytes(filePath, icoBytes);
|
|
}
|
|
|
|
private static byte[] CreateClockIcoBytes(int size)
|
|
{
|
|
if (size <= 0)
|
|
size = 256;
|
|
if (size > 256)
|
|
size = 256;
|
|
if (size < 16)
|
|
size = 16;
|
|
|
|
byte[] pngBytes;
|
|
using (Bitmap bmp = new Bitmap(size, size))
|
|
using (Graphics g = Graphics.FromImage(bmp))
|
|
{
|
|
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
g.Clear(Color.Transparent);
|
|
DrawClock(g, size);
|
|
|
|
using var ms = new MemoryStream();
|
|
bmp.Save(ms, ImageFormat.Png);
|
|
pngBytes = ms.ToArray();
|
|
}
|
|
|
|
using var icoStream = new MemoryStream(6 + 16 + pngBytes.Length);
|
|
using (var bw = new BinaryWriter(icoStream, System.Text.Encoding.UTF8, leaveOpen: true))
|
|
{
|
|
bw.Write((ushort)0);
|
|
bw.Write((ushort)1);
|
|
bw.Write((ushort)1);
|
|
|
|
bw.Write((byte)(size == 256 ? 0 : size));
|
|
bw.Write((byte)(size == 256 ? 0 : size));
|
|
bw.Write((byte)0);
|
|
bw.Write((byte)0);
|
|
bw.Write((ushort)1);
|
|
bw.Write((ushort)32);
|
|
bw.Write((uint)pngBytes.Length);
|
|
bw.Write((uint)(6 + 16));
|
|
}
|
|
|
|
icoStream.Write(pngBytes, 0, pngBytes.Length);
|
|
return icoStream.ToArray();
|
|
}
|
|
|
|
private static void DrawClock(Graphics g, int size)
|
|
{
|
|
int margin = Math.Max(2, size / 16);
|
|
int clockSize = size - margin * 2;
|
|
Rectangle rect = new Rectangle(margin, margin, clockSize, clockSize);
|
|
|
|
using (Brush earBrush = new SolidBrush(Color.White))
|
|
{
|
|
g.FillPie(earBrush, 0, 0, size / 2, size / 2, 200, 50);
|
|
g.FillPie(earBrush, size / 2, 0, size / 2, size / 2, 290, 50);
|
|
}
|
|
|
|
using (Pen pen = new Pen(Color.White, Math.Max(2, size / 16)))
|
|
{
|
|
g.DrawEllipse(pen, rect);
|
|
}
|
|
|
|
Point center = new Point(size / 2, size / 2);
|
|
using (Pen handPen = new Pen(Color.LightGreen, Math.Max(2, size / 22)))
|
|
{
|
|
handPen.EndCap = LineCap.Round;
|
|
g.DrawLine(handPen, center, new Point(center.X + Math.Max(2, size * 10 / 64), center.Y - Math.Max(2, size * 10 / 64)));
|
|
g.DrawLine(handPen, center, new Point(center.X, center.Y - Math.Max(3, size * 18 / 64)));
|
|
}
|
|
}
|
|
}
|
|
}
|