/// <summary> /// Encodes the image to the specified stream from the <see cref="Image{TPixel}"/>. /// </summary> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <param name="image">The <see cref="Image{TPixel}"/> to encode from.</param> /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param> public void Encode <TPixel>(Image <TPixel> image, Stream stream) where TPixel : unmanaged, IPixel <TPixel> { Configuration configuration = Configuration.Default; MemoryAllocator allocator = configuration.MemoryAllocator; using var encoder = new PngEncoderCore(allocator, configuration, new PngEncoderOptions(this)); encoder.Encode(image, stream); }
/// <summary> /// Encodes the image to the specified stream from the <see cref="Image{TPixel}"/>. /// </summary> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <param name="image">The <see cref="Image{TPixel}"/> to encode from.</param> /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task EncodeAsync <TPixel>(Image <TPixel> image, Stream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel <TPixel> { Configuration configuration = Configuration.Default; MemoryAllocator allocator = configuration.MemoryAllocator; // The introduction of a local variable that refers to an object the implements // IDisposable means you must use async/await, where the compiler generates the // state machine and a continuation. using var encoder = new PngEncoderCore(allocator, configuration, new PngEncoderOptions(this)); await encoder.EncodeAsync(image, stream, cancellationToken).ConfigureAwait(false); }