Skip to content

Commit

Permalink
Add a ToBase64 variant MagickImageCollection (#1640)
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Levis <[email protected]>
  • Loading branch information
Gounlaf authored May 24, 2024
1 parent c2ffda6 commit 7954936
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Magick.NET.Core/IMagickImageCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,19 @@ public partial interface IMagickImageCollection : IDisposable
string ToBase64();

/// <summary>
/// Converts this instance to a base64 string.
/// Converts this instance to a base64 <see cref="string"/>.
/// </summary>
/// <param name="format">The format to use.</param>
/// <returns>A base64 <see cref="string"/>.</returns>
string ToBase64(MagickFormat format);

/// <summary>
/// Converts this instance to a base64 <see cref="string"/>.
/// </summary>
/// <param name="defines">The defines to set.</param>
/// <returns>A base64 <see cref="string"/>.</returns>
string ToBase64(IWriteDefines defines);

/// <summary>
/// Determine the overall bounds of all the image layers just as in <see cref="IMagickImageCollection{TQuantumType}.Merge()"/>,
/// then adjust the the canvas and offsets to be relative to those bounds,
Expand Down
11 changes: 11 additions & 0 deletions src/Magick.NET/MagickImageCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,17 @@ public string ToBase64(MagickFormat format)
return ToBase64(bytes);
}

/// <summary>
/// Converts this instance to a base64 <see cref="string"/>.
/// </summary>
/// <param name="defines">The defines to set.</param>
/// <returns>A base64 <see cref="string"/>.</returns>
public string ToBase64(IWriteDefines defines)
{
var bytes = ToByteArray(defines);
return ToBase64(bytes);
}

/// <summary>
/// Determine the overall bounds of all the image layers just as in <see cref="Merge()"/>,
/// then adjust the the canvas and offsets to be relative to those bounds,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
}

0 comments on commit 7954936

Please sign in to comment.