Updated the version.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
14 using System.Drawing.Imaging;
18 namespace OpenHardwareMonitor.Utilities {
19 public class IconFactory {
21 private struct BITMAPINFOHEADER {
26 public ushort BitCount;
27 public uint Compression;
28 public uint SizeImage;
29 public int XPelsPerMeter;
30 public int YPelsPerMeter;
32 public uint ClrImportant;
34 public BITMAPINFOHEADER(int width, int height, int bitCount) {
39 this.BitCount = (ushort)bitCount;
42 this.XPelsPerMeter = 0;
43 this.YPelsPerMeter = 0;
45 this.ClrImportant = 0;
48 public void Write(BinaryWriter bw) {
54 bw.Write(Compression);
56 bw.Write(XPelsPerMeter);
57 bw.Write(YPelsPerMeter);
59 bw.Write(ClrImportant);
63 private struct ICONIMAGE {
64 public BITMAPINFOHEADER Header;
69 public ICONIMAGE(int width, int height, byte[] colors) {
70 this.Header = new BITMAPINFOHEADER(width, height << 1,
71 (8 * colors.Length) / (width * height));
73 int maskSize = (width * height) >> 3;
74 this.XOR = new byte[maskSize];
75 this.AND = new byte[maskSize];
78 public void Write(BinaryWriter bw) {
80 int stride = Header.Width << 2;
81 for (int i = (Header.Height >> 1) - 1; i >= 0; i--)
82 bw.Write(Colors, i * stride, stride);
88 private struct ICONDIRENTRY {
91 public byte ColorCount;
94 public ushort BitCount;
95 public uint BytesInRes;
96 public uint ImageOffset;
98 public ICONDIRENTRY(ICONIMAGE image, int imageOffset) {
99 this.Width = (byte)image.Header.Width;
100 this.Height = (byte)(image.Header.Height >> 1);
103 this.Planes = image.Header.Planes;
104 this.BitCount = image.Header.BitCount;
105 this.BytesInRes = (uint)(image.Header.Size +
106 image.Colors.Length + image.XOR.Length + image.AND.Length);
107 this.ImageOffset = (uint)imageOffset;
110 public void Write(BinaryWriter bw) {
113 bw.Write(ColorCount);
117 bw.Write(BytesInRes);
118 bw.Write(ImageOffset);
126 private struct ICONDIR {
127 public ushort Reserved;
130 public ICONDIRENTRY[] Entries;
132 public ICONDIR(ICONDIRENTRY[] entries) {
135 this.Count = (ushort)entries.Length;
136 this.Entries = entries;
139 public void Write(BinaryWriter bw) {
143 for (int i = 0; i < Entries.Length; i++)
144 Entries[i].Write(bw);
148 get { return (uint)(6 + Entries.Length *
149 (Entries.Length > 0 ? Entries[0].Size : 0)); }
153 public static Icon Create(byte[] colors, int width, int height,
154 PixelFormat format) {
155 if (format != PixelFormat.Format32bppArgb)
156 throw new NotImplementedException();
158 ICONIMAGE image = new ICONIMAGE(width, height, colors);
159 ICONDIR dir = new ICONDIR(
160 new ICONDIRENTRY[] { new ICONDIRENTRY(image, 0) } );
161 dir.Entries[0].ImageOffset = dir.Size;
164 using (BinaryWriter bw = new BinaryWriter(new MemoryStream())) {
168 bw.BaseStream.Position = 0;
169 icon = new Icon(bw.BaseStream);