diff --git a/src/ImageSharp/ImageExtensions.cs b/src/ImageSharp/ImageExtensions.cs index 62ac449b7..e5b2a32a9 100644 --- a/src/ImageSharp/ImageExtensions.cs +++ b/src/ImageSharp/ImageExtensions.cs @@ -7,12 +7,11 @@ using System.IO; using System.Text; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats; -using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp { /// - /// Extension methods over Image{TPixel}. + /// Extension methods for the type. /// public static partial class ImageExtensions { @@ -20,13 +19,13 @@ namespace SixLabors.ImageSharp /// Writes the image to the given stream using the currently loaded image format. /// /// The source image. - /// The file path to save the image to. - /// Thrown if the stream is null. - public static void Save(this Image source, string filePath) + /// The file path to save the image to. + /// The path is null. + public static void Save(this Image source, string path) { - Guard.NotNullOrWhiteSpace(filePath, nameof(filePath)); + Guard.NotNull(path, nameof(path)); - string ext = Path.GetExtension(filePath); + string ext = Path.GetExtension(path); IImageFormat format = source.GetConfiguration().ImageFormatsManager.FindFormatByFileExtension(ext); if (format is null) { @@ -34,7 +33,7 @@ namespace SixLabors.ImageSharp sb.AppendLine($"No encoder was found for extension '{ext}'. Registered encoders include:"); foreach (IImageFormat fmt in source.GetConfiguration().ImageFormats) { - sb.AppendLine($" - {fmt.Name} : {string.Join(", ", fmt.FileExtensions)}"); + sb.AppendFormat(" - {0} : {1}{2}", fmt.Name, string.Join(", ", fmt.FileExtensions), Environment.NewLine); } throw new NotSupportedException(sb.ToString()); @@ -48,26 +47,28 @@ namespace SixLabors.ImageSharp sb.AppendLine($"No encoder was found for extension '{ext}' using image format '{format.Name}'. Registered encoders include:"); foreach (KeyValuePair enc in source.GetConfiguration().ImageFormatsManager.ImageEncoders) { - sb.AppendLine($" - {enc.Key} : {enc.Value.GetType().Name}"); + sb.AppendFormat(" - {0} : {1}{2}", enc.Key, enc.Value.GetType().Name, Environment.NewLine); } throw new NotSupportedException(sb.ToString()); } - source.Save(filePath, encoder); + source.Save(path, encoder); } /// /// Writes the image to the given stream using the currently loaded image format. /// /// The source image. - /// The file path to save the image to. + /// The file path to save the image to. /// The encoder to save the image with. - /// Thrown if the encoder is null. - public static void Save(this Image source, string filePath, IImageEncoder encoder) + /// The path is null. + /// The encoder is null. + public static void Save(this Image source, string path, IImageEncoder encoder) { + Guard.NotNull(path, nameof(path)); Guard.NotNull(encoder, nameof(encoder)); - using (Stream fs = source.GetConfiguration().FileSystem.Create(filePath)) + using (Stream fs = source.GetConfiguration().FileSystem.Create(path)) { source.Save(fs, encoder); } @@ -79,10 +80,20 @@ namespace SixLabors.ImageSharp /// The source image. /// The stream to save the image to. /// The format to save the image in. - /// Thrown if the stream is null. + /// The stream is null. + /// The format is null. + /// The stream is not writable. + /// No encoder available for provided format. public static void Save(this Image source, Stream stream, IImageFormat format) { + Guard.NotNull(stream, nameof(stream)); Guard.NotNull(format, nameof(format)); + + if (!stream.CanWrite) + { + throw new NotSupportedException("Cannot write to the stream."); + } + IImageEncoder encoder = source.GetConfiguration().ImageFormatsManager.FindEncoder(format); if (encoder is null) @@ -92,7 +103,7 @@ namespace SixLabors.ImageSharp foreach (KeyValuePair val in source.GetConfiguration().ImageFormatsManager.ImageEncoders) { - sb.AppendLine($" - {val.Key.Name} : {val.Value.GetType().Name}"); + sb.AppendFormat(" - {0} : {1}{2}", val.Key.Name, val.Value.GetType().Name, Environment.NewLine); } throw new NotSupportedException(sb.ToString()); @@ -113,9 +124,12 @@ namespace SixLabors.ImageSharp /// /// The source image /// The format. + /// The format is null. /// The public static string ToBase64String(this Image source, IImageFormat format) { + Guard.NotNull(format, nameof(format)); + using var stream = new MemoryStream(); source.Save(stream, format);