// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; #if !SUPPORTS_BASE64SPAN using System.Buffers; #endif using System.Collections.Generic; 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}. /// public static partial class ImageExtensions { /// /// 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) { Guard.NotNullOrWhiteSpace(filePath, nameof(filePath)); string ext = Path.GetExtension(filePath); IImageFormat format = source.GetConfiguration().ImageFormatsManager.FindFormatByFileExtension(ext); if (format is null) { var sb = new StringBuilder(); 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)}"); } throw new NotSupportedException(sb.ToString()); } IImageEncoder encoder = source.GetConfiguration().ImageFormatsManager.FindEncoder(format); if (encoder is null) { var sb = new StringBuilder(); 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}"); } throw new NotSupportedException(sb.ToString()); } source.Save(filePath, 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 encoder to save the image with. /// Thrown if the encoder is null. public static void Save(this Image source, string filePath, IImageEncoder encoder) { Guard.NotNull(encoder, nameof(encoder)); using (Stream fs = source.GetConfiguration().FileSystem.Create(filePath)) { source.Save(fs, encoder); } } /// /// Writes the image to the given stream using the currently loaded image format. /// /// The source image. /// The stream to save the image to. /// The format to save the image in. /// Thrown if the stream is null. public static void Save(this Image source, Stream stream, IImageFormat format) { Guard.NotNull(format, nameof(format)); IImageEncoder encoder = source.GetConfiguration().ImageFormatsManager.FindEncoder(format); if (encoder is null) { var sb = new StringBuilder(); sb.AppendLine("No encoder was found for the provided mime type. Registered encoders include:"); foreach (KeyValuePair val in source.GetConfiguration().ImageFormatsManager.ImageEncoders) { sb.AppendLine($" - {val.Key.Name} : {val.Value.GetType().Name}"); } throw new NotSupportedException(sb.ToString()); } source.Save(stream, encoder); } /// /// Returns a Base64 encoded string from the given image. /// /// /// The pixel format. /// The source image /// The format. /// The public static string ToBase64String(this Image source, IImageFormat format) where TPixel : unmanaged, IPixel { using (var stream = new MemoryStream()) { source.Save(stream, format); // Always available. stream.TryGetBuffer(out ArraySegment buffer); #if !SUPPORTS_BASE64SPAN byte[] sharedBuffer = ArrayPool.Shared.Rent(buffer.Count); try { buffer.AsSpan().CopyTo(sharedBuffer); return $"data:{format.DefaultMimeType};base64,{Convert.ToBase64String(sharedBuffer)}"; } finally { ArrayPool.Shared.Return(sharedBuffer); } #else return $"data:{format.DefaultMimeType};base64,{Convert.ToBase64String(buffer)}"; #endif } } } }