/// <summary> /// Initializes a new instance of the <see cref="HeifContext"/> class. /// </summary> /// <exception cref="HeifException"> /// Unable to create the native HeifContext. /// /// -or- /// /// The LibHeif version is not supported. /// </exception> public HeifContext() { LibHeifVersion.ThrowIfNotSupported(); this.context = CreateNativeContext(); this.readerStreamIO = null; }
/// <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.readerStreamIO = HeifStreamFactory.CreateFromFile(path, FileMode.Open, FileAccess.Read, FileShare.Read); InitializeContextFromReader(); } catch { this.context.Dispose(); this.readerStreamIO?.Dispose(); throw; } }
/// <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.readerStreamIO = HeifStreamFactory.CreateFromMemory(bytes); InitializeContextFromReader(); } catch { this.context.Dispose(); this.readerStreamIO?.Dispose(); throw; } }
/// <summary> /// Creates a <see cref="HeifStreamIO"/> instance from the specified byte array. /// </summary> /// <param name="bytes">The byte array.</param> /// <returns>The created <see cref="HeifStreamIO"/> instance.</returns> /// <exception cref="ArgumentNullException"><paramref name="bytes"/> is null.</exception> public static HeifStreamIO CreateFromMemory(byte[] bytes) { HeifStreamIO heifStreamIO; MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(bytes); heifStreamIO = new HeifStreamIO(memoryStream, ownsStream: true); memoryStream = null; } finally { memoryStream?.Dispose(); } return(heifStreamIO); }
/// <summary> /// Creates a <see cref="HeifStreamIO" /> instance from the specified file. /// </summary> /// <param name="path">The path.</param> /// <param name="fileMode">The file mode.</param> /// <param name="fileAccess">The file access.</param> /// <param name="fileShare">The file share mode.</param> /// <returns> /// The created <see cref="HeifStreamIO" /> instance. /// </returns> /// <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="ArgumentOutOfRangeException"><paramref name="fileMode" /> contains an invalid value.</exception> /// <exception cref="FileNotFoundException">The file specified by <paramref name="path" /> does not exist.</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 static HeifStreamIO CreateFromFile(string path, FileMode fileMode, FileAccess fileAccess, FileShare fileShare) { HeifStreamIO heifStreamIO; FileStream fileStream = null; try { fileStream = new FileStream(path, fileMode, fileAccess, fileShare); heifStreamIO = new HeifStreamIO(fileStream, ownsStream: true); fileStream = null; } finally { fileStream?.Dispose(); } return(heifStreamIO); }
/// <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.readerStreamIO = new HeifStreamIO(stream, !leaveOpen); InitializeContextFromReader(); } catch { this.context.Dispose(); this.readerStreamIO?.Dispose(); throw; } }