Пример #1
0
        public override ResourceData FromFile(Stream stream, String extension, ResourceSource source)
        {
            IconDirectoryResourceData rd;

            if (IconDirectoryResourceData.TryCreateFromFile(stream, extension, source, out rd))
            {
                return(rd);
            }

            return(null);
        }
Пример #2
0
        public override ResourceData FromResource(ResourceLang lang, byte[] data)
        {
            IconDirectoryResourceData rd;

            if (IconDirectoryResourceData.TryCreateFromRes(lang, data, out rd))
            {
                return(rd);
            }

            return(null);
        }
Пример #3
0
        public override ResourceData FromFileToUpdate(Stream stream, String extension, ResourceLang currentLang)
        {
            if (!IsExtension(extension, "ICO"))
            {
                throw new ArgumentException("ico is the only supported extension");
            }

            IconDirectoryResourceData originalData = currentLang.Data as IconDirectoryResourceData;

            if (originalData == null)
            {
                throw new ResourceDataException("Unexpected original data subclass");
            }

            IconGroup newGroup = new IconGroup(stream);

            IconGroup canonicalGroup = originalData.IconGroup;

            canonicalGroup.Merge(newGroup, GetMergeOptions());

            return(new IconDirectoryResourceData(canonicalGroup, null));
        }
Пример #4
0
        public static Boolean TryCreateFromRes(ResourceLang lang, Byte[] rawData, out IconDirectoryResourceData typed)
        {
            Int32 sizeOfIconDir = Marshal.SizeOf(typeof(IconDirectory));
            Int32 sizeOfDirEntr = Marshal.SizeOf(typeof(ResIconDirectoryEntry));

            // the data in here is an ICONDIR structure

            IntPtr p = Marshal.AllocHGlobal(rawData.Length);

            Marshal.Copy(rawData, 0, p, rawData.Length);

            // this could be vastly simplified by correctly marshaling the member array of IconDirectory
            // but that's a can of worms, I'd rather not
            IconDirectory dir = (IconDirectory)Marshal.PtrToStructure(p, typeof(IconDirectory));

            Marshal.FreeHGlobal(p);

            if (dir.wType != 1)
            {
                throw new InvalidOperationException("Provided rawData was not that of an icon's");
            }

            ResIconDirectoryEntry[] subImages = new ResIconDirectoryEntry[dir.wCount];

            for (int i = 0; i < dir.wCount; i++)
            {
                Int32 byteOffset = sizeOfIconDir + sizeOfDirEntr * i;

                p = Marshal.AllocHGlobal(sizeOfDirEntr);
                Marshal.Copy(rawData, byteOffset, p, sizeOfDirEntr);

                ResIconDirectoryEntry img = (ResIconDirectoryEntry)Marshal.PtrToStructure(p, typeof(ResIconDirectoryEntry));

                subImages[i] = img;
            }

            IconDirectoryResourceData retval = new IconDirectoryResourceData(lang, rawData);

            // then we might be able to get the resourcedata for the subimages to include in the directory

            // find the Icon Image resource type
            ResourceType iconType = null;

            foreach (ResourceType type in lang.Name.Type.Source.Types)
            {
                if (type.Identifier.KnownType == Win32ResourceType.IconImage)
                {
                    iconType = type;
                    break;
                }
            }

            if (iconType != null)
            {
                foreach (ResIconDirectoryEntry img in subImages)
                {
                    IconCursorImageResourceData rd = GetDataFromWid(iconType, lang, img.wId);

                    String description = String.Format(
                        Cult.InvariantCulture,
                        "{0}x{1} {2}-bit",
                        img.bWidth == 0 ? 256 : img.bWidth,
                        img.bHeight == 0 ? 256 : img.bHeight,
                        img.wBitCount
                        );

                    retval.UnderlyingMembers.Add(new IconDirectoryMember(description, rd));
                }
            }

            typed = retval;
            return(true);
        }
Пример #5
0
        public static Boolean TryCreateFromFile(Stream stream, String extension, ResourceSource source, out IconDirectoryResourceData typed)
        {
            typed = null;

            if (extension != "ico")
            {
                throw MEEx(extension);
            }

            if (stream.Length < Marshal.SizeOf(typeof(IconDirectory)))
            {
                return(false);
            }

            BinaryReader rdr = new BinaryReader(stream);

            IconDirectory?tDir = ReadIcoHeader(rdr);

            if (tDir == null)
            {
                return(false);
            }

            IconDirectory dir = tDir.Value;

            ///////////////////////////////

            // rdr is now at the beginning of the array of FileIconDirectoryMembers

            FileIconDirectoryEntry[] subImages = new FileIconDirectoryEntry[dir.wCount];

            for (int i = 0; i < dir.wCount; i++)
            {
                subImages[i] = ReadFileDirMember(rdr);
            }

            /////////////////////////////

            // now for image data itself

            IconImageResourceDataFactory factory = GetIconImageFactory();

            IconCursorImageResourceData[] images = new IconCursorImageResourceData[dir.wCount];
            String[] descs = new String[dir.wCount];

            for (int i = 0; i < dir.wCount; i++)
            {
                FileIconDirectoryEntry img = subImages[i];

                stream.Seek(img.dwImageOffset, SeekOrigin.Begin);

                Byte[] data = new Byte[img.dwBytesInRes];

                stream.Read(data, 0, (int)img.dwBytesInRes);

                images[i] = factory.FromResource(null, data) as IconCursorImageResourceData;

                String description = String.Format(
                    Cult.InvariantCulture,
                    "{0}x{1} {2}-bit",
                    img.bWidth == 0 ? 256 : img.bWidth,
                    img.bHeight == 0 ? 256 : img.bHeight,
                    img.wBitCount
                    );

                descs[i] = description;
            }

            Byte[] reconstructed = ReconstructRawData(dir, subImages, images, source);

            IconDirectoryResourceData retval = new IconDirectoryResourceData(null, reconstructed);

            for (int i = 0; i < images.Length; i++)
            {
                retval.UnderlyingMembers.Add(new IconDirectoryMember(descs[i], images[i]));
            }


            /////////////////////////////

            typed = retval;

            return(true);
        }