Browse Source

Merge branch 'master' into SaveAsExtensions

pull/1574/head
Anton Firszov 6 years ago
committed by GitHub
parent
commit
6944b593e8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 54
      src/ImageSharp/Image.FromFile.cs
  2. 29
      tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs

54
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 @@ namespace SixLabors.ImageSharp
public static Image Load(string path)
=> Load(Configuration.Default, path);
/// <summary>
/// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary>
/// <param name="path">The file path to the image.</param>
/// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable.
/// </exception>
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
public static Task<Image> LoadAsync(string path)
=> LoadAsync(Configuration.Default, path);
/// <summary>
/// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary>
@ -114,6 +126,25 @@ namespace SixLabors.ImageSharp
public static Image Load(Configuration configuration, string path)
=> Load(configuration, path, out _);
/// <summary>
/// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary>
/// <param name="configuration">The configuration for the decoder.</param>
/// <param name="path">The file path to the image.</param>
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
/// <exception cref="ArgumentNullException">The path is null.</exception>
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
public static async Task<Image> LoadAsync(Configuration configuration, string path)
{
using (Stream stream = configuration.FileSystem.OpenRead(path))
{
(Image img, _) = await LoadWithFormatAsync(configuration, stream).ConfigureAwait(false);
return img;
}
}
/// <summary>
/// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary>
@ -137,6 +168,29 @@ namespace SixLabors.ImageSharp
}
}
/// <summary>
/// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary>
/// <param name="configuration">The Configuration.</param>
/// <param name="path">The file path to the image.</param>
/// <param name="decoder">The decoder.</param>
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
/// <exception cref="ArgumentNullException">The path is null.</exception>
/// <exception cref="ArgumentNullException">The decoder is null.</exception>
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
public static Task<Image> 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);
}
}
/// <summary>
/// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary>

29
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 @@ namespace SixLabors.ImageSharp.Tests
}
}
[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 @@ namespace SixLabors.ImageSharp.Tests
}
}
[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()
{

Loading…
Cancel
Save