Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HeifContext"/> class with the specified stream to read from, and optionally leaves the stream open.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <param name="leaveOpen">
        /// <see langword="true"/> to leave the stream open after the <see cref="HeifContext"/> object is disposed; otherwise, <see langword="false"/>.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="stream"/> must support reading and seeking.</exception>
        /// <exception cref="HeifException">
        /// Unable to create the native HeifContext.
        ///
        /// -or-
        ///
        /// The LibHeif version is not supported.
        ///
        /// -or-
        ///
        /// A LibHeif error occurred.
        /// </exception>
        public HeifContext(Stream stream, bool leaveOpen = false)
        {
            Validate.IsNotNull(stream, nameof(stream));
            LibHeifVersion.ThrowIfNotSupported();

            if (!stream.CanRead || !stream.CanSeek)
            {
                ExceptionUtil.ThrowArgumentException(Resources.StreamCannotReadAndSeek);
            }

            this.context = CreateNativeContext();
            try
            {
                this.reader = HeifReaderFactory.CreateFromStream(stream, !leaveOpen);
                InitializeContextFromReader();
            }
            catch (Exception ex)
            {
                this.context.Dispose();
                this.reader?.Dispose();

                if (ex is ArgumentException || ex is HeifException)
                {
                    throw;
                }
                else
                {
                    throw new HeifException(ex.Message, ex);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HeifContext"/> class, with the specified file to read from.
        /// </summary>
        /// <param name="path">The file to read from.</param>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="path"/> is empty, contains only whitespace or contains invalid characters.
        /// </exception>
        /// <exception cref="FileNotFoundException">The file specified by <paramref name="path"/> does not exist.</exception>
        /// <exception cref="HeifException">
        /// Unable to create the native HeifContext.
        ///
        /// -or-
        ///
        /// The LibHeif version is not supported.
        ///
        /// -or-
        ///
        /// A LibHeif error occurred.
        /// </exception>
        /// <exception cref="IOException">An I/O error occurred.</exception>
        /// <exception cref="System.Security.SecurityException">The caller does not have the required permission.</exception>
        /// <exception cref="UnauthorizedAccessException">
        /// The access requested is not permitted by the operating system for the specified path.
        /// </exception>
        public HeifContext(string path)
        {
            Validate.IsNotNull(path, nameof(path));
            LibHeifVersion.ThrowIfNotSupported();

            this.context = CreateNativeContext();
            try
            {
                this.reader = HeifReaderFactory.CreateFromFile(path);
                InitializeContextFromReader();
            }
            catch
            {
                this.context.Dispose();
                this.reader?.Dispose();
                throw;
            }
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HeifContext"/> class, with the specified byte array to read from.
        /// </summary>
        /// <param name="bytes">A byte array that contains the HEIF image.</param>
        /// <exception cref="ArgumentNullException"><paramref name="bytes"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="bytes"/> is an empty array.</exception>
        /// <exception cref="HeifException">
        /// Unable to create the native HeifContext.
        ///
        /// -or-
        ///
        /// The LibHeif version is not supported.
        ///
        /// -or-
        ///
        /// A LibHeif error occurred.
        /// </exception>
        public HeifContext(byte[] bytes)
        {
            Validate.IsNotNullOrEmptyArray(bytes, nameof(bytes));
            LibHeifVersion.ThrowIfNotSupported();

            this.context = CreateNativeContext();
            try
            {
                this.reader = HeifReaderFactory.CreateFromMemory(bytes);
                InitializeContextFromReader();
            }
            catch
            {
                this.context.Dispose();
                this.reader?.Dispose();
                throw;
            }
        }