From 795493633c05ba757c7d4828e64f2f1fd4b5f9b9 Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Fri, 24 May 2024 18:53:01 +0200 Subject: [PATCH] Add a ToBase64 variant MagickImageCollection (#1640) Signed-off-by: Florian Levis --- src/Magick.NET.Core/IMagickImageCollection.cs | 9 +++++++- src/Magick.NET/MagickImageCollection.cs | 11 ++++++++++ .../TheToBase64Method.cs | 22 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Magick.NET.Core/IMagickImageCollection.cs b/src/Magick.NET.Core/IMagickImageCollection.cs index 2e66ae7189..a582ce9658 100644 --- a/src/Magick.NET.Core/IMagickImageCollection.cs +++ b/src/Magick.NET.Core/IMagickImageCollection.cs @@ -383,12 +383,19 @@ public partial interface IMagickImageCollection : IDisposable string ToBase64(); /// - /// Converts this instance to a base64 string. + /// Converts this instance to a base64 . /// /// The format to use. /// A base64 . string ToBase64(MagickFormat format); + /// + /// Converts this instance to a base64 . + /// + /// The defines to set. + /// A base64 . + string ToBase64(IWriteDefines defines); + /// /// Determine the overall bounds of all the image layers just as in , /// then adjust the the canvas and offsets to be relative to those bounds, diff --git a/src/Magick.NET/MagickImageCollection.cs b/src/Magick.NET/MagickImageCollection.cs index 9801c58f81..3fba3e0b25 100644 --- a/src/Magick.NET/MagickImageCollection.cs +++ b/src/Magick.NET/MagickImageCollection.cs @@ -1349,6 +1349,17 @@ public string ToBase64(MagickFormat format) return ToBase64(bytes); } + /// + /// Converts this instance to a base64 . + /// + /// The defines to set. + /// A base64 . + public string ToBase64(IWriteDefines defines) + { + var bytes = ToByteArray(defines); + return ToBase64(bytes); + } + /// /// Determine the overall bounds of all the image layers just as in , /// then adjust the the canvas and offsets to be relative to those bounds, diff --git a/tests/Magick.NET.Tests/MagickImageCollectionTests/TheToBase64Method.cs b/tests/Magick.NET.Tests/MagickImageCollectionTests/TheToBase64Method.cs index 0456bc4ac7..b6f1397881 100644 --- a/tests/Magick.NET.Tests/MagickImageCollectionTests/TheToBase64Method.cs +++ b/tests/Magick.NET.Tests/MagickImageCollectionTests/TheToBase64Method.cs @@ -1,7 +1,9 @@ // Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET. // Licensed under the Apache License, Version 2.0. +using System; using ImageMagick; +using ImageMagick.Formats; using Xunit; namespace Magick.NET.Tests; @@ -27,5 +29,25 @@ public void ShouldReturnBase64StringOfTheImages() var base64 = images.ToBase64(MagickFormat.Rgb); Assert.Equal(1228800, base64.Length); } + + [Fact] + public void ShouldReturnBase64EncodedStringUsingTheSpecifiedDefines() + { + using var images = new MagickImageCollection(); + images.Read(Files.Builtin.Logo); + + var defines = new TiffWriteDefines + { + PreserveCompression = true, + }; + var base64 = images.ToBase64(defines); + + Assert.NotNull(base64); + Assert.Equal(39952, base64.Length); + + var bytes = Convert.FromBase64String(base64); + + Assert.NotNull(bytes); + } } }