Example #1
0
        public ImageSource GetIconImage(ResourceID id, bool icon)
        {
            IntPtr iconName;

            using (var d = id.GetAsIntPtr(out iconName)) {
                var hIcon = Win32.LoadImage(_hModule, iconName, icon ? Win32.ImageType.Icon : Win32.ImageType.Cursor, 0, 0, Win32.LoadImageFlags.None);
                if (hIcon == IntPtr.Zero)
                {
                    var bytes = GetResourceContent(id, icon ? ResourceID.Icon : ResourceID.Cursor);
                    hIcon = Win32.CreateIconFromResource(bytes, bytes.Length, icon, 0x30000);
                    if (hIcon == IntPtr.Zero)
                    {
                        return(null);
                    }
                }

                var source = Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                if (icon)
                {
                    Win32.DestroyIcon(hIcon);
                }
                else
                {
                    Win32.DestroyCursor(hIcon);
                }

                return(source);
            }
        }
Example #2
0
        public byte[] GetResourceContent(ResourceID name, ResourceID type)
        {
            IntPtr iname, itype;

            using (var d1 = name.GetAsIntPtr(out iname)) {
                using (var d2 = type.GetAsIntPtr(out itype)) {
                    var hResource = Win32.FindResource(_hModule, iname, itype);
                    if (hResource == IntPtr.Zero)
                    {
                        return(null);
                    }

                    var hGlobal = Win32.LoadResource(_hModule, hResource);
                    if (hGlobal == IntPtr.Zero)
                    {
                        return(null);
                    }

                    var size = Win32.SizeofResource(_hModule, hResource);
                    var ptr  = Win32.LockResource(hGlobal);

                    var buffer = new byte[size];
                    Marshal.Copy(ptr, buffer, 0, buffer.Length);
                    return(buffer);
                }
            }
        }