Пример #1
0
        /// <summary>
        /// Loads the specified image from a file.
        /// </summary>
        /// <param name="fileName">The filename.</param>
        /// <returns>An new image.</returns>
        /// <remarks>This method support the following format: <c>dds, bmp, jpg, png, gif, tiff, wmp, tga</c>.</remarks>
        public static Image Load(string fileName)
        {
            NativeFileStream stream    = null;
            IntPtr           memoryPtr = IntPtr.Zero;
            int size;

            try
            {
                stream    = new NativeFileStream(fileName, NativeFileMode.Open, NativeFileAccess.Read);
                size      = (int)stream.Length;
                memoryPtr = Utilities.AllocateMemory(size);
                stream.Read(memoryPtr, 0, size);
            }
            catch (Exception)
            {
                if (memoryPtr != IntPtr.Zero)
                {
                    Utilities.FreeMemory(memoryPtr);
                }
                throw;
            }
            finally
            {
                try
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                } catch {}
            }

            // If everything was fine, load the image from memory
            return(Load(memoryPtr, size, false));
        }
Пример #2
0
        /// <summary>
        /// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
        /// </summary>
        /// <param name="path">The file to open for reading. </param>
        /// <returns>A byte array containing the contents of the file.</returns>
        public static byte[] ReadAllBytes(string path)
        {
            byte[] buffer;
            using (var stream = new NativeFileStream(path, NativeFileMode.Open, NativeFileAccess.Read))
            {
                int  offset = 0;
                long length = stream.Length;
                if (length > 0x7fffffffL)
                {
                    throw new IOException("File too long");
                }

                int count = (int)length;
                buffer = new byte[count];

                while (count > 0)
                {
                    int num4 = stream.Read(buffer, offset, count);
                    if (num4 == 0)
                    {
                        throw new EndOfStreamException();
                    }
                    offset += num4;
                    count  -= num4;
                }
            }
            return(buffer);
        }
Пример #3
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException();
            }
            if (!_canRead)
            {
                throw new NotSupportedException();
            }

            lock (_nativeFileStream)
            {
                // argument validation in interop layer
                return(_nativeFileStream.Read(buffer, offset, count, NativeFileStream.TimeoutDefault));
            }
        }