Meme Generation API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Kynareth/src/Managers/ImageManager.Magik.cs

58 lines
1.3 KiB

using System.Text;
using Exsersewo.Common.Utilities;
using ImageMagick;
namespace Kynareth.Managers;
public static partial class ImageManager
{
readonly static Encoding s_defaultEncoding = Encoding.Unicode;
const double c_defaultFontSize = 20;
public static async Task<(MemoryStream data, string contentType)> GetLiquidRescaledImageAsync(string image)
{
var imageStream = await HttpWebClient.GetStreamAsync(new Uri(image)).ConfigureAwait(false);
using var magikImage = new MagickImageCollection(imageStream);
Parallel.ForEach(magikImage, LiquidResizeFrame);
MemoryStream stream = new();
magikImage.Write(stream);
stream.Position = 0;
magikImage.Dispose();
string cType = "image/png";
if (magikImage.Count > 1) cType = "image/gif";
return (stream, cType);
}
static void LiquidResizeFrame(IMagickImage<byte> frame)
{
int originalWidth = frame.Width;
int originalHeight = frame.Height;
frame.Resize(800, 600);
frame.LiquidRescale(new MagickGeometry
{
X = 1,
Width = Convert.ToInt32(frame.Width * 0.5),
Height = Convert.ToInt32(frame.Height * 0.5)
});
frame.LiquidRescale(new MagickGeometry
{
X = 2,
Width = Convert.ToInt32(frame.Width * 1.5),
Height = Convert.ToInt32(frame.Height * 1.5)
});
frame.Resize(originalWidth, originalHeight);
}
}