Beispiel #1
0
        /// <summary>Read a single image into a byte array.</summary>
        byte[] read(int i)
        {
            ICONDIRECTORY dir = m_images[i];

            if (stream.CanSeek)
            {
                stream.Seek(dir.imageOffset, SeekOrigin.Begin);
            }
            else
            {
                if (streamPosition > dir.imageOffset)
                {
                    throw new ArgumentException("The stream is forward-only, can't rewind it");
                }
                int skipBytes = dir.imageOffset - streamPosition;
                stream.skip(skipBytes);
                streamPosition += skipBytes;
            }

            int cb = dir.sizeInBytes;

            byte[] result = new byte[cb];
            if (cb != stream.Read(result, 0, cb))
            {
                throw new EndOfStreamException();
            }
            if (!stream.CanSeek)
            {
                streamPosition += cb;
            }
            return(result);
        }
Beispiel #2
0
            internal ImageFormat(ref ICONDIRECTORY icd, uint bmp)
            {
                size        = icd.size;
                colorsCount = icd.colorsCount;
                planes      = icd.planes;
                bitCount    = icd.bitCount;
                switch (bmp)
                {
                case 40:
                    format = eFormat.Bitmap;
                    break;

                case PngSignature:
                    format = eFormat.PNG;
                    break;

                default:
                    throw new ArgumentException("The image format is not supported");
                }
            }
Beispiel #3
0
        /// <summary>Read file header and directory</summary>
        public CursorFile(Stream stream, bool leaveOpen = false)
        {
            Debug.Assert(6 == Marshal.SizeOf <ICONDIR>());

            ICONDIR header = new ICONDIR();

            stream.Read(MiscUtils.asSpan(ref header));

            if (header.idReserved != 0)
            {
                throw new ArgumentException("The stream is not a valid cursor file");
            }

            if (header.idType != eImageType.Cursor)
            {
                if (header.idType == eImageType.Icon)
                {
                    throw new ArgumentException("The stream contains an icon, not a cursor");
                }
                throw new ArgumentException("The stream is not a valid cursor file");
            }
            if (header.idCount <= 0)
            {
                throw new ArgumentException("The stream doesn't have any images");
            }

            Debug.Assert(16 == Marshal.SizeOf <ICONDIRECTORY>());
            m_images = new ICONDIRECTORY[header.idCount];
            stream.read(m_images);

            this.stream    = stream;
            this.leaveOpen = leaveOpen;
            if (!stream.CanSeek)
            {
                streamPosition = Marshal.SizeOf <ICONDIR>() + Marshal.SizeOf <ICONDIRECTORY>() * m_images.Length;
            }
        }
Beispiel #4
0
        static ulong headerValues(ref ICONDIRECTORY icd)
        {
            var bytes = MiscUtils.asSpan(ref icd).Slice(0, 8);

            return(MemoryMarshal.Cast <byte, ulong>(bytes)[0]);
        }
Beispiel #5
0
 internal ImageInfo(ICONDIRECTORY idi)
 {
     size        = idi.size;
     hotspot     = idi.hotspot;
     colorsCount = idi.colorsCount;
 }