From 742d98a4c11eb3dbb271e9875e73b4caa3fe94d9 Mon Sep 17 00:00:00 2001 From: Toastie Date: Wed, 4 Sep 2024 22:02:50 +1200 Subject: [PATCH] Updated image library --- src/EllieBot/EllieBot.csproj | 6 +-- .../Administration/AutoPublishService.cs | 2 + .../Gambling/PlantPick/PlantPickService.cs | 42 +++++++++++-------- .../Modules/Gambling/Slot/SlotCommands.cs | 6 +-- .../Modules/Searches/Crypto/CryptoService.cs | 2 +- .../ImagesharpStockChartDrawingService.cs | 8 ++-- src/EllieBot/Modules/Xp/XpService.cs | 27 ++++++------ .../_common/JsonConverters/Rgba32Converter.cs | 2 +- .../_common/_Extensions/Rgba32Extensions.cs | 2 +- 9 files changed, 54 insertions(+), 43 deletions(-) diff --git a/src/EllieBot/EllieBot.csproj b/src/EllieBot/EllieBot.csproj index b67381c..910d7d1 100644 --- a/src/EllieBot/EllieBot.csproj +++ b/src/EllieBot/EllieBot.csproj @@ -69,9 +69,9 @@ - - - + + + diff --git a/src/EllieBot/Modules/Administration/AutoPublishService.cs b/src/EllieBot/Modules/Administration/AutoPublishService.cs index 8f29495..a0983d3 100644 --- a/src/EllieBot/Modules/Administration/AutoPublishService.cs +++ b/src/EllieBot/Modules/Administration/AutoPublishService.cs @@ -37,6 +37,8 @@ public class AutoPublishService : IExecNoCommand, IReadyExecutor, IEService }); } + // todo GUILDS + public async Task OnReadyAsync() { var creds = _creds.GetCreds(); diff --git a/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs b/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs index bffd035..37aba9f 100644 --- a/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs +++ b/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs @@ -111,13 +111,17 @@ public class PlantPickService : IEService, IExecNoCommand { var curImg = await _images.GetCurrencyImageAsync(); + if (curImg is null) + return (new MemoryStream(), null); + if (string.IsNullOrWhiteSpace(pass)) { // determine the extension - using var load = _ = Image.Load(curImg, out var format); + using var load = Image.Load(curImg); + var format = load.Metadata.DecodedImageFormat; // return the image - return (curImg.ToStream(), format.FileExtensions.FirstOrDefault() ?? "png"); + return (curImg.ToStream(), format?.FileExtensions.FirstOrDefault() ?? "png"); } // get the image stream and extension @@ -134,16 +138,17 @@ public class PlantPickService : IEService, IExecNoCommand { // draw lower, it looks better pass = pass.TrimTo(10, true).ToLowerInvariant(); - using var img = Image.Load(curImg, out var format); + using var img = Image.Load(curImg); // choose font size based on the image height, so that it's visible var font = _fonts.NotoSans.CreateFont(img.Height / 12.0f, FontStyle.Bold); img.Mutate(x => { // measure the size of the text to be drawing - var size = TextMeasurer.Measure(pass, new TextOptions(font) - { - Origin = new PointF(0, 0) - }); + var size = TextMeasurer.MeasureSize(pass, + new TextOptions(font) + { + Origin = new PointF(0, 0) + }); // fill the background with black, add 5 pixels on each side to make it look better x.FillPolygon(Color.ParseHex("00000080"), @@ -156,6 +161,7 @@ public class PlantPickService : IEService, IExecNoCommand x.DrawText(pass, font, Color.White, new(0, 0)); }); // return image as a stream for easy sending + var format = img.Metadata.DecodedImageFormat; return (img.ToStream(format), format.FileExtensions.FirstOrDefault() ?? "png"); } @@ -256,7 +262,8 @@ public class PlantPickService : IEService, IExecNoCommand pass = pass?.Trim().TrimTo(10, true).ToUpperInvariant(); // gets all plants in this channel with the same password - var entries = uow.Set().AsQueryable() + var entries = uow.Set() + .AsQueryable() .Where(x => x.ChannelId == ch.Id && pass == x.Password) .ToList(); // sum how much currency that is, and get all of the message ids (so that i can delete them) @@ -368,15 +375,16 @@ public class PlantPickService : IEService, IExecNoCommand string pass) { await using var uow = _db.GetDbContext(); - uow.Set().Add(new() - { - Amount = amount, - GuildId = gid, - ChannelId = cid, - Password = pass, - UserId = uid, - MessageId = mid - }); + uow.Set() + .Add(new() + { + Amount = amount, + GuildId = gid, + ChannelId = cid, + Password = pass, + UserId = uid, + MessageId = mid + }); await uow.SaveChangesAsync(); } } \ No newline at end of file diff --git a/src/EllieBot/Modules/Gambling/Slot/SlotCommands.cs b/src/EllieBot/Modules/Gambling/Slot/SlotCommands.cs index a5c7465..18c294a 100644 --- a/src/EllieBot/Modules/Gambling/Slot/SlotCommands.cs +++ b/src/EllieBot/Modules/Gambling/Slot/SlotCommands.cs @@ -172,13 +172,13 @@ public partial class Gambling } var slotBg = await _images.GetSlotBgAsync(); - var bgImage = Image.Load(slotBg, out _); + var bgImage = Image.Load(slotBg); var numbers = new int[3]; result.Rolls.CopyTo(numbers, 0); Color fontColor = Config.Slots.CurrencyFontColor; - bgImage.Mutate(x => x.DrawText(new TextOptions(_fonts.DottyFont.CreateFont(65)) + bgImage.Mutate(x => x.DrawText(new RichTextOptions(_fonts.DottyFont.CreateFont(65)) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, @@ -190,7 +190,7 @@ public partial class Gambling var bottomFont = _fonts.DottyFont.CreateFont(50); - bgImage.Mutate(x => x.DrawText(new TextOptions(bottomFont) + bgImage.Mutate(x => x.DrawText(new RichTextOptions(bottomFont) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, diff --git a/src/EllieBot/Modules/Searches/Crypto/CryptoService.cs b/src/EllieBot/Modules/Searches/Crypto/CryptoService.cs index f5271d6..0ffd422 100644 --- a/src/EllieBot/Modules/Searches/Crypto/CryptoService.cs +++ b/src/EllieBot/Modules/Searches/Crypto/CryptoService.cs @@ -90,7 +90,7 @@ public class CryptoService : IEService img.Mutate(x => { - x.DrawLines(color, 2, points); + x.DrawLine(color, 2, points); }); return img; diff --git a/src/EllieBot/Modules/Searches/Crypto/Drawing/ImagesharpStockChartDrawingService.cs b/src/EllieBot/Modules/Searches/Crypto/Drawing/ImagesharpStockChartDrawingService.cs index 731fc78..95aa51f 100644 --- a/src/EllieBot/Modules/Searches/Crypto/Drawing/ImagesharpStockChartDrawingService.cs +++ b/src/EllieBot/Modules/Searches/Crypto/Drawing/ImagesharpStockChartDrawingService.cs @@ -94,7 +94,7 @@ public sealed class ImagesharpStockChartDrawingService : IStockChartDrawingServi => image.Mutate(ctx => { foreach (var data in drawData) - ctx.DrawLines(data.IsGreen + ctx.DrawLine(data.IsGreen ? _greenBrush : _redBrush, 1, @@ -128,7 +128,7 @@ public sealed class ImagesharpStockChartDrawingService : IStockChartDrawingServi { // draw guides foreach (var y in lines) - ctx.DrawLines(_lineGuideColor, 1, new PointF(0, y), new PointF(WIDTH, y)); + ctx.DrawLine(_lineGuideColor, 1, new PointF(0, y), new PointF(WIDTH, y)); // // draw min and max price on the chart // ctx.DrawText(min.ToString(CultureInfo.InvariantCulture), @@ -156,7 +156,7 @@ public sealed class ImagesharpStockChartDrawingService : IStockChartDrawingServi image.Mutate(ctx => { - ctx.DrawLines(_sparklineColor, 2, points); + ctx.DrawLine(_sparklineColor, 2, points); }); return Task.FromResult(new("png", image.ToStream())); @@ -177,7 +177,7 @@ public sealed class ImagesharpStockChartDrawingService : IStockChartDrawingServi var points = GetSparklinePointsInternal(series); image.Mutate(ctx => { - ctx.DrawLines(Color.ParseHex("00FFFFAA"), 1, points); + ctx.DrawLine(Color.ParseHex("00FFFFAA"), 1, points); }); return Task.FromResult(new("png", image.ToStream())); diff --git a/src/EllieBot/Modules/Xp/XpService.cs b/src/EllieBot/Modules/Xp/XpService.cs index f32c7a5..32ba10e 100644 --- a/src/EllieBot/Modules/Xp/XpService.cs +++ b/src/EllieBot/Modules/Xp/XpService.cs @@ -994,23 +994,23 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand throw new ArgumentNullException(nameof(bgBytes)); } - var outlinePen = new Pen(Color.Black, 1f); + var outlinePen = new SolidPen(Color.Black, 1f); - using var img = Image.Load(bgBytes, out var imageFormat); + using var img = Image.Load(bgBytes); if (template.User.Name.Show) { var fontSize = (int)(template.User.Name.FontSize * 0.9); var username = stats.User.ToString(); var usernameFont = _fonts.NotoSans.CreateFont(fontSize, FontStyle.Bold); - var size = TextMeasurer.Measure($"@{username}", new(usernameFont)); + var size = TextMeasurer.MeasureSize($"@{username}", new(usernameFont)); var scale = 400f / size.Width; if (scale < 1) usernameFont = _fonts.NotoSans.CreateFont(template.User.Name.FontSize * scale, FontStyle.Bold); img.Mutate(x => { - x.DrawText(new TextOptions(usernameFont) + x.DrawText(new RichTextOptions(usernameFont) { HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, @@ -1031,7 +1031,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand var clubFont = _fonts.NotoSans.CreateFont(template.Club.Name.FontSize, FontStyle.Regular); - img.Mutate(x => x.DrawText(new TextOptions(clubFont) + img.Mutate(x => x.DrawText(new RichTextOptions(clubFont) { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top, @@ -1051,7 +1051,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand int maxSize) { var font = fontFamily.CreateFont(fontSize, style); - var size = TextMeasurer.Measure(text, new(font)); + var size = TextMeasurer.MeasureSize(text, new(font)); var scale = maxSize / size.Width; if (scale < 1) font = fontFamily.CreateFont(fontSize * scale, style); @@ -1114,7 +1114,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand if (template.User.Xp.Global.Show) { img.Mutate(x => x.DrawText( - new TextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Global.FontSize, FontStyle.Bold)) + new RichTextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Global.FontSize, FontStyle.Bold)) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, @@ -1128,7 +1128,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand if (template.User.Xp.Guild.Show) { img.Mutate(x => x.DrawText( - new TextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Guild.FontSize, FontStyle.Bold)) + new RichTextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Guild.FontSize, FontStyle.Bold)) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, @@ -1152,7 +1152,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand new(awX, awY))); } - var rankPen = new Pen(Color.White, 1); + var rankPen = new SolidPen(Color.White, 1); //ranking if (template.User.GlobalRank.Show) { @@ -1166,7 +1166,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand 68); img.Mutate(x => x.DrawText( - new TextOptions(globalRankFont) + new RichTextOptions(globalRankFont) { Origin = new(template.User.GlobalRank.Pos.X, template.User.GlobalRank.Pos.Y) }, @@ -1188,7 +1188,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand 43); img.Mutate(x => x.DrawText( - new TextOptions(guildRankFont) + new RichTextOptions(guildRankFont) { Origin = new(template.User.GuildRank.Pos.X, template.User.GuildRank.Pos.Y) }, @@ -1231,7 +1231,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand } using var toDraw = Image.Load(data); - if (toDraw.Size() != new Size(template.User.Icon.Size.X, template.User.Icon.Size.Y)) + if (toDraw.Size != new Size(template.User.Icon.Size.X, template.User.Icon.Size.Y)) toDraw.Mutate(x => x.Resize(template.User.Icon.Size.X, template.User.Icon.Size.Y)); img.Mutate(x => x.DrawImage(toDraw, @@ -1257,6 +1257,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand if (outputSize.X != img.Width || outputSize.Y != img.Height) img.Mutate(x => x.Resize(template.OutputSize.X, template.OutputSize.Y)); + var imageFormat = img.Metadata.DecodedImageFormat; var output = ((Stream)await img.ToStreamAsync(imageFormat), imageFormat); return output; @@ -1393,7 +1394,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand } using var toDraw = Image.Load(data); - if (toDraw.Size() != new Size(template.Club.Icon.Size.X, template.Club.Icon.Size.Y)) + if (toDraw.Size != new Size(template.Club.Icon.Size.X, template.Club.Icon.Size.Y)) toDraw.Mutate(x => x.Resize(template.Club.Icon.Size.X, template.Club.Icon.Size.Y)); img.Mutate(x => x.DrawImage( diff --git a/src/EllieBot/_common/JsonConverters/Rgba32Converter.cs b/src/EllieBot/_common/JsonConverters/Rgba32Converter.cs index 906db1e..4b2a313 100644 --- a/src/EllieBot/_common/JsonConverters/Rgba32Converter.cs +++ b/src/EllieBot/_common/JsonConverters/Rgba32Converter.cs @@ -7,7 +7,7 @@ namespace EllieBot.Common.JsonConverters; public class Rgba32Converter : JsonConverter { public override Rgba32 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - => Rgba32.ParseHex(reader.GetString()); + => Rgba32.ParseHex(reader.GetString()!); public override void Write(Utf8JsonWriter writer, Rgba32 value, JsonSerializerOptions options) => writer.WriteStringValue(value.ToHex()); diff --git a/src/EllieBot/_common/_Extensions/Rgba32Extensions.cs b/src/EllieBot/_common/_Extensions/Rgba32Extensions.cs index a0cd408..6fb4c7c 100644 --- a/src/EllieBot/_common/_Extensions/Rgba32Extensions.cs +++ b/src/EllieBot/_common/_Extensions/Rgba32Extensions.cs @@ -24,7 +24,7 @@ public static class Rgba32Extensions using var frame = imgArray[i].Frames.CloneFrame(frameNumber % imgArray[i].Frames.Count); var offset = xOffset; imgFrame.Mutate(x => x.DrawImage(frame, new Point(offset, 0), new GraphicsOptions())); - xOffset += imgArray[i].Bounds().Width; + xOffset += imgArray[i].Bounds.Width; } }