Skip to content

Commit 4a48355

Browse files
authored
Merge pull request #1257 from pekspro/LoadExtensions
LoadAsync methods accepting string path.
2 parents f80a960 + f0d9784 commit 4a48355

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

src/ImageSharp/Image.FromFile.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.IO;
6+
using System.Threading.Tasks;
67
using SixLabors.ImageSharp.Formats;
78
using SixLabors.ImageSharp.PixelFormats;
89

@@ -89,6 +90,17 @@ public static IImageInfo Identify(Configuration configuration, string filePath,
8990
public static Image Load(string path)
9091
=> Load(Configuration.Default, path);
9192

93+
/// <summary>
94+
/// Create a new instance of the <see cref="Image"/> class from the given file.
95+
/// </summary>
96+
/// <param name="path">The file path to the image.</param>
97+
/// <exception cref="NotSupportedException">
98+
/// Thrown if the stream is not readable nor seekable.
99+
/// </exception>
100+
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
101+
public static Task<Image> LoadAsync(string path)
102+
=> LoadAsync(Configuration.Default, path);
103+
92104
/// <summary>
93105
/// Create a new instance of the <see cref="Image"/> class from the given file.
94106
/// </summary>
@@ -114,6 +126,25 @@ public static Image Load(string path, out IImageFormat format)
114126
public static Image Load(Configuration configuration, string path)
115127
=> Load(configuration, path, out _);
116128

129+
/// <summary>
130+
/// Create a new instance of the <see cref="Image"/> class from the given file.
131+
/// </summary>
132+
/// <param name="configuration">The configuration for the decoder.</param>
133+
/// <param name="path">The file path to the image.</param>
134+
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
135+
/// <exception cref="ArgumentNullException">The path is null.</exception>
136+
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
137+
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
138+
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
139+
public static async Task<Image> LoadAsync(Configuration configuration, string path)
140+
{
141+
using (Stream stream = configuration.FileSystem.OpenRead(path))
142+
{
143+
(Image img, _) = await LoadWithFormatAsync(configuration, stream).ConfigureAwait(false);
144+
return img;
145+
}
146+
}
147+
117148
/// <summary>
118149
/// Create a new instance of the <see cref="Image"/> class from the given file.
119150
/// </summary>
@@ -137,6 +168,29 @@ public static Image Load(Configuration configuration, string path, IImageDecoder
137168
}
138169
}
139170

171+
/// <summary>
172+
/// Create a new instance of the <see cref="Image"/> class from the given file.
173+
/// </summary>
174+
/// <param name="configuration">The Configuration.</param>
175+
/// <param name="path">The file path to the image.</param>
176+
/// <param name="decoder">The decoder.</param>
177+
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
178+
/// <exception cref="ArgumentNullException">The path is null.</exception>
179+
/// <exception cref="ArgumentNullException">The decoder is null.</exception>
180+
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
181+
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
182+
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
183+
public static Task<Image> LoadAsync(Configuration configuration, string path, IImageDecoder decoder)
184+
{
185+
Guard.NotNull(configuration, nameof(configuration));
186+
Guard.NotNull(path, nameof(path));
187+
188+
using (Stream stream = configuration.FileSystem.OpenRead(path))
189+
{
190+
return LoadAsync(configuration, stream, decoder);
191+
}
192+
}
193+
140194
/// <summary>
141195
/// Create a new instance of the <see cref="Image"/> class from the given file.
142196
/// </summary>

tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5-
5+
using System.Threading.Tasks;
66
using SixLabors.ImageSharp.Formats;
77
using SixLabors.ImageSharp.Formats.Bmp;
88
using SixLabors.ImageSharp.PixelFormats;
@@ -40,6 +40,24 @@ public void Path_Agnostic()
4040
}
4141
}
4242

43+
[Fact]
44+
public async Task Path_Agnostic_Async()
45+
{
46+
using (var img = await Image.LoadAsync(this.Path))
47+
{
48+
VerifyDecodedImage(img);
49+
}
50+
}
51+
52+
[Fact]
53+
public async Task Path_Agnostic_Configuration_Async()
54+
{
55+
using (var img = await Image.LoadAsync(Configuration.Default, this.Path))
56+
{
57+
VerifyDecodedImage(img);
58+
}
59+
}
60+
4361
[Fact]
4462
public void Path_Decoder_Specific()
4563
{
@@ -58,6 +76,15 @@ public void Path_Decoder_Agnostic()
5876
}
5977
}
6078

79+
[Fact]
80+
public async Task Path_Decoder_Agnostic_Async()
81+
{
82+
using (var img = await Image.LoadAsync(Configuration.Default, this.Path, new BmpDecoder()))
83+
{
84+
VerifyDecodedImage(img);
85+
}
86+
}
87+
6188
[Fact]
6289
public void Path_OutFormat_Specific()
6390
{

0 commit comments

Comments
 (0)