Exemplo n.º 1
0
        private static IconImageResourceDataFactory GetIconImageFactory()
        {
            IconImageResourceDataFactory factory = null;

            ResourceTypeIdentifier typeId = new ResourceTypeIdentifier(new IntPtr((int)Win32ResourceType.IconImage));

            ResourceDataFactory[] factories = ResourceDataFactory.GetFactoriesForType(typeId);

            foreach (ResourceDataFactory f in factories)
            {
                if (f is IconImageResourceDataFactory)
                {
                    factory = f as IconImageResourceDataFactory;
                    break;
                }
            }

            if (factory == null)
            {
                throw ME(new ApplicationException("Unable to locate IconImageResourceDataFactory"));
            }

            return(factory);
        }
Exemplo n.º 2
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);
        }