diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs index 24d187830..129b3a1aa 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs @@ -57,7 +57,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// public async Task DecodeAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken) => await this.DecodeAsync(configuration, stream, cancellationToken) - .ConfigureAwait(false); + .ConfigureAwait(false); /// public IImageInfo Identify(Configuration configuration, Stream stream) diff --git a/src/ImageSharp/Formats/Gif/GifDecoder.cs b/src/ImageSharp/Formats/Gif/GifDecoder.cs index f665cc0ab..196d77ad7 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoder.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoder.cs @@ -49,7 +49,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// public async Task DecodeAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken) => await this.DecodeAsync(configuration, stream, cancellationToken) - .ConfigureAwait(false); + .ConfigureAwait(false); /// public IImageInfo Identify(Configuration configuration, Stream stream) diff --git a/src/ImageSharp/Formats/ImageDecoderUtilities.cs b/src/ImageSharp/Formats/ImageDecoderUtilities.cs index a5f5819bd..5d77fb0c8 100644 --- a/src/ImageSharp/Formats/ImageDecoderUtilities.cs +++ b/src/ImageSharp/Formats/ImageDecoderUtilities.cs @@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Formats { try { - using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream); + using var bufferedReadStream = new BufferedReadStream(configuration, stream); IImageInfo imageInfo = decoder.Identify(bufferedReadStream, cancellationToken); return Task.FromResult(imageInfo); } @@ -108,7 +108,7 @@ namespace SixLabors.ImageSharp.Formats { try { - using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream); + using var bufferedReadStream = new BufferedReadStream(configuration, stream); Image image = decoder.Decode(bufferedReadStream, cancellationToken); return Task.FromResult(image); } @@ -132,7 +132,7 @@ namespace SixLabors.ImageSharp.Formats Configuration configuration, Stream stream) { - using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream); + using var bufferedReadStream = new BufferedReadStream(configuration, stream); try { @@ -155,7 +155,7 @@ namespace SixLabors.ImageSharp.Formats Func largeImageExceptionFactory) where TPixel : unmanaged, IPixel { - using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream); + using var bufferedReadStream = new BufferedReadStream(configuration, stream); try { diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs index 93697db95..39b8e492f 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs @@ -44,7 +44,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// public async Task DecodeAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken) - => await this.DecodeAsync(configuration, stream, cancellationToken).ConfigureAwait(false); + => await this.DecodeAsync(configuration, stream, cancellationToken) + .ConfigureAwait(false); /// public IImageInfo Identify(Configuration configuration, Stream stream) @@ -56,12 +57,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg } /// - public Task IdentifyAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken) + public async Task IdentifyAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken) { Guard.NotNull(stream, nameof(stream)); - using var decoder = new JpegDecoderCore(configuration, this); - return decoder.IdentifyAsync(configuration, stream, cancellationToken); + // 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 decoder = new JpegDecoderCore(configuration, this)) + { + return await decoder.IdentifyAsync(configuration, stream, cancellationToken) + .ConfigureAwait(false); + } } } } diff --git a/src/ImageSharp/Formats/Png/PngDecoder.cs b/src/ImageSharp/Formats/Png/PngDecoder.cs index 1ee47a017..479c24b7e 100644 --- a/src/ImageSharp/Formats/Png/PngDecoder.cs +++ b/src/ImageSharp/Formats/Png/PngDecoder.cs @@ -4,8 +4,6 @@ using System.IO; using System.Threading; using System.Threading.Tasks; -using SixLabors.ImageSharp.IO; -using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Png @@ -39,7 +37,8 @@ namespace SixLabors.ImageSharp.Formats.Png /// public async Task DecodeAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken) - => await this.DecodeAsync(configuration, stream, cancellationToken); + => await this.DecodeAsync(configuration, stream, cancellationToken) + .ConfigureAwait(false); /// public IImageInfo Identify(Configuration configuration, Stream stream) diff --git a/src/ImageSharp/Formats/Png/PngEncoder.cs b/src/ImageSharp/Formats/Png/PngEncoder.cs index 7e563596e..e72e8d3d5 100644 --- a/src/ImageSharp/Formats/Png/PngEncoder.cs +++ b/src/ImageSharp/Formats/Png/PngEncoder.cs @@ -77,6 +77,9 @@ namespace SixLabors.ImageSharp.Formats.Png public async Task EncodeAsync(Image image, Stream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { + // 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(image.GetMemoryAllocator(), image.GetConfiguration(), new PngEncoderOptions(this))) { await encoder.EncodeAsync(image, stream, cancellationToken).ConfigureAwait(false); diff --git a/src/ImageSharp/Formats/Tga/TgaDecoder.cs b/src/ImageSharp/Formats/Tga/TgaDecoder.cs index bad769fa9..e06a0ee88 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoder.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoder.cs @@ -41,7 +41,8 @@ namespace SixLabors.ImageSharp.Formats.Tga /// public async Task DecodeAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken) - => await this.DecodeAsync(configuration, stream, cancellationToken); + => await this.DecodeAsync(configuration, stream, cancellationToken) + .ConfigureAwait(false); /// public IImageInfo Identify(Configuration configuration, Stream stream)