diff --git a/src/ImageSharp/Image.FromFile.cs b/src/ImageSharp/Image.FromFile.cs index 3237ea7430..a078f2db98 100644 --- a/src/ImageSharp/Image.FromFile.cs +++ b/src/ImageSharp/Image.FromFile.cs @@ -3,6 +3,7 @@ using System; using System.IO; +using System.Threading.Tasks; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.PixelFormats; @@ -89,6 +90,17 @@ public static IImageInfo Identify(Configuration configuration, string filePath, public static Image Load(string path) => Load(Configuration.Default, path); + /// + /// Create a new instance of the class from the given file. + /// + /// The file path to the image. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A representing the asynchronous operation. + public static Task LoadAsync(string path) + => LoadAsync(Configuration.Default, path); + /// /// Create a new instance of the class from the given file. /// @@ -114,6 +126,25 @@ public static Image Load(string path, out IImageFormat format) public static Image Load(Configuration configuration, string path) => Load(configuration, path, out _); + /// + /// Create a new instance of the class from the given file. + /// + /// The configuration for the decoder. + /// The file path to the image. + /// The configuration is null. + /// The path is null. + /// Image format not recognised. + /// Image contains invalid content. + /// A representing the asynchronous operation. + public static async Task LoadAsync(Configuration configuration, string path) + { + using (Stream stream = configuration.FileSystem.OpenRead(path)) + { + (Image img, _) = await LoadWithFormatAsync(configuration, stream).ConfigureAwait(false); + return img; + } + } + /// /// Create a new instance of the class from the given file. /// @@ -137,6 +168,29 @@ public static Image Load(Configuration configuration, string path, IImageDecoder } } + /// + /// Create a new instance of the class from the given file. + /// + /// The Configuration. + /// The file path to the image. + /// The decoder. + /// The configuration is null. + /// The path is null. + /// The decoder is null. + /// Image format not recognised. + /// Image contains invalid content. + /// A representing the asynchronous operation. + public static Task LoadAsync(Configuration configuration, string path, IImageDecoder decoder) + { + Guard.NotNull(configuration, nameof(configuration)); + Guard.NotNull(path, nameof(path)); + + using (Stream stream = configuration.FileSystem.OpenRead(path)) + { + return LoadAsync(configuration, stream, decoder); + } + } + /// /// Create a new instance of the class from the given file. /// diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs index 5f7137e151..77e5679f64 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs @@ -2,7 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; - +using System.Threading.Tasks; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.PixelFormats; @@ -40,6 +40,24 @@ public void Path_Agnostic() } } + [Fact] + public async Task Path_Agnostic_Async() + { + using (var img = await Image.LoadAsync(this.Path)) + { + VerifyDecodedImage(img); + } + } + + [Fact] + public async Task Path_Agnostic_Configuration_Async() + { + using (var img = await Image.LoadAsync(Configuration.Default, this.Path)) + { + VerifyDecodedImage(img); + } + } + [Fact] public void Path_Decoder_Specific() { @@ -58,6 +76,15 @@ public void Path_Decoder_Agnostic() } } + [Fact] + public async Task Path_Decoder_Agnostic_Async() + { + using (var img = await Image.LoadAsync(Configuration.Default, this.Path, new BmpDecoder())) + { + VerifyDecodedImage(img); + } + } + [Fact] public void Path_OutFormat_Specific() {