diff --git a/Src/Workshell.PE.Resources/Graphics/CursorResource.cs b/Src/Workshell.PE.Resources/Graphics/CursorResource.cs index 4a556f4..941a5e1 100644 --- a/Src/Workshell.PE.Resources/Graphics/CursorResource.cs +++ b/Src/Workshell.PE.Resources/Graphics/CursorResource.cs @@ -42,11 +42,13 @@ namespace Workshell.PE.Resources public sealed class CursorInfo { - internal CursorInfo(CursorResource cursor, uint languageId, ushort hotspotX, ushort hotspotY, byte[] dib) + internal CursorInfo(CursorResource cursor, uint languageId, ushort hotspotX, ushort hotspotY, ushort width, ushort height, byte colors, byte[] dib) { Cursor = cursor; Language = languageId; Hotspot = new Point(hotspotX, hotspotY); + Size = new Size(width, height); + Colors = colors; DIB = dib; } @@ -70,6 +72,18 @@ public Point Hotspot private set; } + public Size Size + { + get; + private set; + } + + public byte Colors + { + get; + private set; + } + public byte[] DIB { get; @@ -85,7 +99,8 @@ public enum CursorSaveFormat Raw, Cursor, Icon, - Bitmap + Bitmap, + PNG } public class CursorResource : Resource @@ -116,32 +131,30 @@ public CursorInfo GetInfo() public CursorInfo GetInfo(uint languageId) { Tuple tuple = Load(languageId); - CursorInfo result = new CursorInfo(this, languageId, tuple.Item1, tuple.Item2, tuple.Item3); - return result; - } + using (MemoryStream dib_mem = new MemoryStream(tuple.Item3)) + { + BITMAPINFOHEADER header = Utils.Read(dib_mem); - public Icon ToIcon() - { - return ToIcon(Color.Transparent); - } + ushort width = Convert.ToUInt16(header.biWidth); + ushort height = Convert.ToUInt16(header.biHeight / 2); + byte color_count = Convert.ToByte(header.biBitCount); + CursorInfo result = new CursorInfo(this, languageId, tuple.Item1, tuple.Item2, width, height, color_count, tuple.Item3); - public Icon ToIcon(Color backgroundColor) - { - return ToIcon(DEFAULT_LANGUAGE, Color.Transparent); + return result; + } } - public Icon ToIcon(uint languageId) + public Icon ToIcon() { - return ToIcon(languageId, Color.Transparent); + return ToIcon(DEFAULT_LANGUAGE); } - public Icon ToIcon(uint languageId, Color backgroundColor) + public Icon ToIcon(uint languageId) { Tuple tuple = Load(languageId); - MemoryStream dib_mem = new MemoryStream(tuple.Item3); - using (dib_mem) + using (MemoryStream dib_mem = new MemoryStream(tuple.Item3)) { BITMAPINFOHEADER header = Utils.Read(dib_mem); @@ -199,7 +212,7 @@ public Bitmap ToBitmap(uint languageId) public Bitmap ToBitmap(uint languageId, Color backgroundColor) { - using (Icon icon = ToIcon(languageId, backgroundColor)) + using (Icon icon = ToIcon(languageId)) { Rectangle rect = new Rectangle(0, 0, icon.Width, icon.Height); Bitmap bitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); @@ -207,12 +220,13 @@ public Bitmap ToBitmap(uint languageId, Color backgroundColor) using (Graphics graphics = Graphics.FromImage(bitmap)) { using (SolidBrush brush = new SolidBrush(backgroundColor)) - { graphics.FillRectangle(brush, rect); - graphics.DrawIcon(icon, rect); - } + + graphics.DrawIcon(icon, rect); } + bitmap.MakeTransparent(backgroundColor); + return bitmap; } } @@ -249,6 +263,15 @@ public void Save(Stream stream, uint languageId, CursorSaveFormat format) bitmap.Save(stream, ImageFormat.Bmp); } + break; + } + case CursorSaveFormat.PNG: + { + using (Bitmap bitmap = ToBitmap(languageId)) + { + bitmap.Save(stream, ImageFormat.Png); + } + break; } case CursorSaveFormat.Cursor: diff --git a/Src/Workshell.PE.Resources/Graphics/IconResource.cs b/Src/Workshell.PE.Resources/Graphics/IconResource.cs index fdce295..4deb74e 100644 --- a/Src/Workshell.PE.Resources/Graphics/IconResource.cs +++ b/Src/Workshell.PE.Resources/Graphics/IconResource.cs @@ -47,9 +47,8 @@ internal IconInfo(IconResource resource, uint languageId, ushort width, ushort h { Icon = resource; Language = languageId; - Width = width; - Height = height; - ColorCount = colors; + Size = new Size(width, height); + Colors = colors; DIB = dib; IsPNG = isPNG; } @@ -68,19 +67,13 @@ public uint Language private set; } - public ushort Width + public Size Size { get; private set; } - public ushort Height - { - get; - private set; - } - - public byte ColorCount + public byte Colors { get; private set; @@ -106,7 +99,8 @@ public enum IconSaveFormat { Raw, Icon, - Bitmap + Bitmap, + PNG } public class IconResource : Resource @@ -144,61 +138,96 @@ public IconInfo GetInfo(uint languageId) public Icon ToIcon() { - return ToIcon(Color.Transparent); - } - - public Icon ToIcon(Color backgroundColor) - { - return ToIcon(DEFAULT_LANGUAGE, Color.Transparent); + return ToIcon(DEFAULT_LANGUAGE); } public Icon ToIcon(uint languageId) - { - return ToIcon(languageId, Color.Transparent); - } - - public Icon ToIcon(uint languageId, Color backgroundColor) { Tuple tuple = Load(languageId); - MemoryStream dib_mem = new MemoryStream(tuple.Item4); - using (dib_mem) + if (!tuple.Item5) { - BITMAPINFOHEADER header = Utils.Read(dib_mem); + using (MemoryStream dib_mem = new MemoryStream(tuple.Item4)) + { + BITMAPINFOHEADER header = Utils.Read(dib_mem); + + ushort width = Convert.ToUInt16(header.biWidth); + ushort height = Convert.ToUInt16(header.biHeight / 2); + byte color_count = Convert.ToByte(header.biBitCount); - ushort width = Convert.ToUInt16(header.biWidth); - ushort height = Convert.ToUInt16(header.biHeight / 2); - byte color_count = Convert.ToByte(header.biBitCount); + MemoryStream mem = new MemoryStream(); - MemoryStream mem = new MemoryStream(); + using (mem) + { + Utils.Write(Convert.ToUInt16(0), mem); + Utils.Write(Convert.ToUInt16(1), mem); + Utils.Write(Convert.ToUInt16(1), mem); - using (mem) + uint colors = 0; + + if (color_count != 0 && color_count < 8) + colors = Convert.ToUInt32(Math.Pow(2, color_count)); + + Utils.Write(Convert.ToByte(width >= 256 ? 0 : width), mem); + Utils.Write(Convert.ToByte(height >= 256 ? 0 : height), mem); + Utils.Write(Convert.ToByte(colors), mem); + Utils.Write(Convert.ToByte(0), mem); + Utils.Write(Convert.ToUInt16(1), mem); + Utils.Write(Convert.ToUInt16(color_count), mem); + Utils.Write(tuple.Item4.Length, mem); + Utils.Write(22, mem); + + Utils.Write(tuple.Item4, mem); + + mem.Seek(0, SeekOrigin.Begin); + + Icon icon = new Icon(mem); + + return icon; + } + } + } + else + { + using (MemoryStream dib_mem = new MemoryStream(tuple.Item4)) { - Utils.Write(Convert.ToUInt16(0), mem); - Utils.Write(Convert.ToUInt16(1), mem); - Utils.Write(Convert.ToUInt16(1), mem); + using (Image png = Image.FromStream(dib_mem)) + { + ushort width = Convert.ToUInt16(png.Width); + ushort height = Convert.ToUInt16(png.Height); + byte color_count = 32; + + MemoryStream mem = new MemoryStream(); - uint colors = 0; + using (mem) + { + Utils.Write(Convert.ToUInt16(0), mem); + Utils.Write(Convert.ToUInt16(1), mem); + Utils.Write(Convert.ToUInt16(1), mem); - if (color_count != 0 && color_count < 8) - colors = Convert.ToUInt32(Math.Pow(2, color_count)); + uint colors = 0; - Utils.Write(Convert.ToByte(width >= 256 ? 0 : width), mem); - Utils.Write(Convert.ToByte(height >= 256 ? 0 : height), mem); - Utils.Write(Convert.ToByte(colors), mem); - Utils.Write(Convert.ToByte(0), mem); - Utils.Write(Convert.ToUInt16(1), mem); - Utils.Write(Convert.ToUInt16(color_count), mem); - Utils.Write(tuple.Item4.Length, mem); - Utils.Write(22, mem); + if (color_count != 0 && color_count < 8) + colors = Convert.ToUInt32(Math.Pow(2, color_count)); - Utils.Write(tuple.Item4, mem); + Utils.Write(Convert.ToByte(width >= 256 ? 0 : width), mem); + Utils.Write(Convert.ToByte(height >= 256 ? 0 : height), mem); + Utils.Write(Convert.ToByte(colors), mem); + Utils.Write(Convert.ToByte(0), mem); + Utils.Write(Convert.ToUInt16(1), mem); + Utils.Write(Convert.ToUInt16(color_count), mem); + Utils.Write(tuple.Item4.Length, mem); + Utils.Write(22, mem); - mem.Seek(0, SeekOrigin.Begin); + Utils.Write(tuple.Item4, mem); - Icon icon = new Icon(mem); + mem.Seek(0, SeekOrigin.Begin); - return icon; + Icon icon = new Icon(mem); + + return icon; + } + } } } } @@ -220,21 +249,40 @@ public Bitmap ToBitmap(uint languageId) public Bitmap ToBitmap(uint languageId, Color backgroundColor) { - using (Icon icon = ToIcon(languageId, backgroundColor)) - { - Rectangle rect = new Rectangle(0, 0, icon.Width, icon.Height); - Bitmap bitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); + Tuple tuple = Load(languageId); - using (Graphics graphics = Graphics.FromImage(bitmap)) + if (!tuple.Item5) + { + using (Icon icon = ToIcon(languageId)) { - using (SolidBrush brush = new SolidBrush(backgroundColor)) + Rectangle rect = new Rectangle(0, 0, icon.Width, icon.Height); + Bitmap bitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); + + using (Graphics graphics = Graphics.FromImage(bitmap)) { - graphics.FillRectangle(brush, rect); + + using (SolidBrush brush = new SolidBrush(backgroundColor)) + graphics.FillRectangle(brush, rect); + graphics.DrawIcon(icon, rect); } + + bitmap.MakeTransparent(backgroundColor); + + return bitmap; } + } + else + { + using (MemoryStream dib_mem = new MemoryStream(tuple.Item4)) + { + using (Image png = Image.FromStream(dib_mem)) + { + Bitmap bitmap = new Bitmap(png); - return bitmap; + return bitmap; + } + } } } @@ -271,6 +319,24 @@ public void Save(Stream stream, uint languageId, IconSaveFormat format) bitmap.Save(stream, ImageFormat.Bmp); } + break; + } + case IconSaveFormat.PNG: + { + Tuple tuple = Load(languageId); + + if (!tuple.Item5) + { + using (Bitmap bitmap = ToBitmap(languageId)) + { + bitmap.Save(stream, ImageFormat.Png); + } + } + else + { + stream.Write(tuple.Item4, 0, tuple.Item4.Length); + } + break; } }