public static Boolean TryCreate(Boolean isIcon, ResourceLang lang, Byte[] rawData, out IconCursorImageResourceData typed) { // rawData is an ICONIMAGE structure OR a PNG image // if it's a PNG image it's easy enough: if (PngImageResourceData.HasPngSignature(rawData)) { Image image; if (!ImageResourceData.TryCreateImage(rawData, out image)) { typed = null; return(false); } typed = new IconCursorImageResourceData(image, lang, rawData) { IsIcon = isIcon }; return(true); } typed = new IconCursorImageResourceData(lang, rawData) { IsIcon = isIcon }; return(true); }
public static Boolean TryCreate(Boolean isIcon, ResourceLang lang, Byte[] rawData, out String message, out IconCursorImageResourceData typed) { message = null; // rawData is an ICONIMAGE structure OR a PNG image // if it's a PNG image it's easy enough: if (PngImageResourceData.HasPngSignature(rawData)) { Image image; if (!ImageResourceData.TryCreateImage(rawData, out image)) { typed = null; return(false); } typed = new IconCursorImageResourceData(IntPtr.Zero, image, lang, rawData) { Size = image.Size }; return(true); } // "Almost" cheating; this uses Win32's icon function, but in a nice way that doesn't break my conceptual model // and to think I'd need to manually process the DIB information to extract and recreate Bitmaps // although I might want to do that in future if I wanted to display the AND and XOR masks separately IntPtr p = Marshal.AllocHGlobal(rawData.Length); Marshal.Copy(rawData, 0, p, rawData.Length); IntPtr hIcon = NativeMethods.CreateIconFromResource(p, (uint)rawData.Length, isIcon); if (hIcon == IntPtr.Zero) { message = NativeMethods.GetLastErrorString(); typed = null; return(false); } Icon icon = Icon.FromHandle(hIcon); // because the Icon is born out of unmanaged data I cannot free the handle here; do it in the finaliser // on Windows XP x86 this doesn't cause a problem by freeing it here, but on Windows Server 2008 x64 it causes the Icon to fail // UPDATE: okay, apparently not. Even after moving that free instruction it still fails on WS2008x64 typed = new IconCursorImageResourceData(p, icon, lang, rawData); return(true); }