Updated imagesharp

This commit is contained in:
Master Kwoth 2017-07-18 18:26:55 +02:00
parent 55b1c3945b
commit 661d026973
9 changed files with 44 additions and 37 deletions

View File

@ -11,6 +11,7 @@ using System.Threading.Tasks;
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
using Image = ImageSharp.Image;
using ImageSharp;
namespace NadekoBot.Modules.Gambling
{
@ -19,8 +20,8 @@ namespace NadekoBot.Modules.Gambling
[Group]
public class DriceRollCommands : NadekoSubmodule
{
private Regex dndRegex { get; } = new Regex(@"^(?<n1>\d+)d(?<n2>\d+)(?:\+(?<add>\d+))?(?:\-(?<sub>\d+))?$", RegexOptions.Compiled);
private Regex fudgeRegex { get; } = new Regex(@"^(?<n1>\d+)d(?:F|f)$", RegexOptions.Compiled);
private readonly Regex dndRegex = new Regex(@"^(?<n1>\d+)d(?<n2>\d+)(?:\+(?<add>\d+))?(?:\-(?<sub>\d+))?$", RegexOptions.Compiled);
private readonly Regex fudgeRegex = new Regex(@"^(?<n1>\d+)d(?:F|f)$", RegexOptions.Compiled);
private readonly char[] _fateRolls = { '-', ' ', '+' };
private readonly IImagesService _images;
@ -42,7 +43,7 @@ namespace NadekoBot.Modules.Gambling
var imageStream = await Task.Run(() =>
{
var ms = new MemoryStream();
new[] { GetDice(num1), GetDice(num2) }.Merge().Save(ms);
new[] { GetDice(num1), GetDice(num2) }.Merge().SaveAsPng(ms);
ms.Position = 0;
return ms;
}).ConfigureAwait(false);
@ -97,7 +98,7 @@ namespace NadekoBot.Modules.Gambling
var rng = new NadekoRandom();
var dice = new List<Image>(num);
var dice = new List<Image<Rgba32>>(num);
var values = new List<int>(num);
for (var i = 0; i < num; i++)
{
@ -127,7 +128,7 @@ namespace NadekoBot.Modules.Gambling
var bitmap = dice.Merge();
var ms = new MemoryStream();
bitmap.Save(ms);
bitmap.SaveAsPng(ms);
ms.Position = 0;
await Context.Channel.SendFileAsync(ms, "dice.png",
Context.User.Mention + " " +
@ -213,7 +214,7 @@ namespace NadekoBot.Modules.Gambling
await ReplyConfirmLocalized("dice_rolled", Format.Bold(rolled.ToString())).ConfigureAwait(false);
}
private Image GetDice(int num)
private Image<Rgba32> GetDice(int num)
{
if (num < 0 || num > 10)
throw new ArgumentOutOfRangeException(nameof(num));
@ -224,15 +225,15 @@ namespace NadekoBot.Modules.Gambling
using (var imgOneStream = images[1].ToStream())
using (var imgZeroStream = images[0].ToStream())
{
Image imgOne = new Image(imgOneStream);
Image imgZero = new Image(imgZeroStream);
var imgOne = Image.Load(imgOneStream);
var imgZero = Image.Load(imgZeroStream);
return new[] { imgOne, imgZero }.Merge();
}
}
using (var die = _images.Dice[num].ToStream())
{
return new Image(die);
return Image.Load(die);
}
}
}

View File

@ -9,6 +9,7 @@ using System.Threading.Tasks;
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Gambling.Common;
using Image = ImageSharp.Image;
using ImageSharp;
namespace NadekoBot.Modules.Gambling
{
@ -27,7 +28,7 @@ namespace NadekoBot.Modules.Gambling
throw new ArgumentOutOfRangeException(nameof(num));
Cards cards = guildId == null ? new Cards() : _allDecks.GetOrAdd(Context.Guild, (s) => new Cards());
var images = new List<Image>();
var images = new List<Image<Rgba32>>();
var cardObjects = new List<Cards.Card>();
for (var i = 0; i < num; i++)
{
@ -46,10 +47,10 @@ namespace NadekoBot.Modules.Gambling
var currentCard = cards.DrawACard();
cardObjects.Add(currentCard);
using (var stream = File.OpenRead(Path.Combine(_cardsPath, currentCard.ToString().ToLowerInvariant() + ".jpg").Replace(' ', '_')))
images.Add(new Image(stream));
images.Add(Image.Load(stream));
}
MemoryStream bitmapStream = new MemoryStream();
images.Merge().Save(bitmapStream);
images.Merge().SaveAsPng(bitmapStream);
bitmapStream.Position = 0;
var toSend = $"{Context.User.Mention}";

View File

@ -9,6 +9,7 @@ using System.Threading.Tasks;
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
using Image = ImageSharp.Image;
using ImageSharp;
namespace NadekoBot.Modules.Gambling
{
@ -56,7 +57,7 @@ namespace NadekoBot.Modules.Gambling
await ReplyErrorLocalized("flip_invalid", 10).ConfigureAwait(false);
return;
}
var imgs = new Image[count];
var imgs = new Image<Rgba32>[count];
for (var i = 0; i < count; i++)
{
using (var heads = _images.Heads.ToStream())
@ -64,11 +65,11 @@ namespace NadekoBot.Modules.Gambling
{
if (rng.Next(0, 10) < 5)
{
imgs[i] = new Image(heads);
imgs[i] = Image.Load(heads);
}
else
{
imgs[i] = new Image(tails);
imgs[i] = Image.Load(tails);
}
}
}

View File

@ -12,6 +12,7 @@ using System.Threading;
using System.Threading.Tasks;
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
using SixLabors.Primitives;
namespace NadekoBot.Modules.Gambling
{
@ -166,7 +167,7 @@ namespace NadekoBot.Modules.Gambling
Interlocked.Add(ref _totalBet, amount);
using (var bgFileStream = _images.SlotBackground.ToStream())
{
var bgImage = new ImageSharp.Image(bgFileStream);
var bgImage = ImageSharp.Image.Load(bgFileStream);
var result = SlotMachine.Pull();
int[] numbers = result.Numbers;
@ -174,7 +175,7 @@ namespace NadekoBot.Modules.Gambling
for (int i = 0; i < 3; i++)
{
using (var file = _images.SlotEmojis[numbers[i]].ToStream())
using (var randomImage = new ImageSharp.Image(file))
using (var randomImage = ImageSharp.Image.Load(file))
{
bgImage.DrawImage(randomImage, 100, default(Size), new Point(95 + 142 * i, 330));
}
@ -187,7 +188,7 @@ namespace NadekoBot.Modules.Gambling
{
var digit = printWon % 10;
using (var fs = _images.SlotNumbers[digit].ToStream())
using (var img = new ImageSharp.Image(fs))
using (var img = ImageSharp.Image.Load(fs))
{
bgImage.DrawImage(img, 100, default(Size), new Point(230 - n * 16, 462));
}
@ -200,7 +201,7 @@ namespace NadekoBot.Modules.Gambling
{
var digit = printAmount % 10;
using (var fs = _images.SlotNumbers[digit].ToStream())
using (var img = new ImageSharp.Image(fs))
using (var img = ImageSharp.Image.Load(fs))
{
bgImage.DrawImage(img, 100, default(Size), new Point(395 - n * 16, 462));
}

View File

@ -7,6 +7,7 @@ using NadekoBot.Common;
using NadekoBot.Extensions;
using NadekoBot.Services;
using NLog;
using SixLabors.Primitives;
namespace NadekoBot.Modules.Games.Common
{
@ -32,7 +33,7 @@ namespace NadekoBot.Modules.Games.Common
try
{
using (var ms = new MemoryStream(_images.WifeMatrix.ToArray(), false))
using (var img = new ImageSharp.Image(ms))
using (var img = Image.Load(ms))
{
const int minx = 35;
const int miny = 385;
@ -42,7 +43,7 @@ namespace NadekoBot.Modules.Games.Common
var pointy = (int)(miny - length * ((Crazy - 4) / 6));
using (var pointMs = new MemoryStream(_images.RategirlDot.ToArray(), false))
using (var pointImg = new ImageSharp.Image(pointMs))
using (var pointImg = Image.Load(pointMs))
{
img.DrawImage(pointImg, 100, default(Size), new Point(pointx - 10, pointy - 10));
}
@ -51,7 +52,7 @@ namespace NadekoBot.Modules.Games.Common
using (var http = new HttpClient())
using (var imgStream = new MemoryStream())
{
img.Save(imgStream);
img.SaveAsPng(imgStream);
var byteContent = new ByteArrayContent(imgStream.ToArray());
http.AddFakeHeaders();

View File

@ -390,7 +390,7 @@ namespace NadekoBot.Modules.Searches
try
{
var items = JArray.Parse(response).Shuffle().ToList();
var images = new List<ImageSharp.Image>();
var images = new List<Image<Rgba32>>();
if (items == null)
throw new KeyNotFoundException("Cannot find a card by that name");
foreach (var item in items.Where(item => item.HasValues && item["img"] != null).Take(4))
@ -402,7 +402,7 @@ namespace NadekoBot.Modules.Searches
var imgStream = new MemoryStream();
await sr.CopyToAsync(imgStream);
imgStream.Position = 0;
images.Add(new ImageSharp.Image(imgStream));
images.Add(ImageSharp.Image.Load(imgStream));
}
}).ConfigureAwait(false);
}
@ -412,7 +412,7 @@ namespace NadekoBot.Modules.Searches
msg = GetText("hs_over_x", 4);
}
var ms = new MemoryStream();
await Task.Run(() => images.AsEnumerable().Merge().Save(ms));
await Task.Run(() => images.AsEnumerable().Merge().SaveAsPng(ms));
ms.Position = 0;
await Context.Channel.SendFileAsync(ms, arg + ".png", msg).ConfigureAwait(false);
}
@ -634,10 +634,10 @@ namespace NadekoBot.Modules.Searches
color = color?.Trim().Replace("#", "");
if (string.IsNullOrWhiteSpace(color))
return;
ImageSharp.Color clr;
Rgba32 clr;
try
{
clr = ImageSharp.Color.FromHex(color);
clr = Rgba32.FromHex(color);
}
catch
{
@ -646,7 +646,7 @@ namespace NadekoBot.Modules.Searches
}
var img = new ImageSharp.Image(50, 50);
var img = new ImageSharp.Image<Rgba32>(50, 50);
img.BackgroundColor(clr);

View File

@ -59,10 +59,10 @@ namespace NadekoBot.Modules.Utility
}
return;
}
var hexColors = hexes.Select(hex =>
{
try { return (ImageSharp.Color?)ImageSharp.Color.FromHex(hex.Replace("#", "")); } catch { return null; }
try { return (Rgba32?)Rgba32.FromHex(hex.Replace("#", "")); } catch { return null; }
})
.Where(c => c != null)
.Select(c => c.Value)
@ -76,7 +76,7 @@ namespace NadekoBot.Modules.Utility
var images = hexColors.Select(color =>
{
var img = new ImageSharp.Image(50, 50);
var img = new ImageSharp.Image<Rgba32>(50, 50);
img.BackgroundColor(color);
return img;
}).Merge().ToStream();

View File

@ -64,8 +64,8 @@
<PackageReference Include="Google.Apis.Urlshortener.v1" Version="1.19.0.138" />
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.20.0.701" />
<PackageReference Include="Google.Apis.Customsearch.v1" Version="1.20.0.466" />
<PackageReference Include="ImageSharp" Version="1.0.0-alpha4-00031" />
<PackageReference Include="ImageSharp.Drawing" Version="1.0.0-alpha4-00031" />
<PackageReference Include="ImageSharp" Version="1.0.0-alpha9-00171" />
<PackageReference Include="ImageSharp.Drawing" Version="1.0.0-alpha9-00166" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.1" />

View File

@ -15,6 +15,8 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NadekoBot.Common.Collections;
using SixLabors.Primitives;
using ImageSharp.PixelFormats;
namespace NadekoBot.Extensions
{
@ -131,10 +133,10 @@ namespace NadekoBot.Extensions
public static string ToJson<T>(this T any, Formatting formatting = Formatting.Indented) =>
JsonConvert.SerializeObject(any, formatting);
public static Stream ToStream(this ImageSharp.Image img)
public static Stream ToStream(this ImageSharp.Image<Rgba32> img)
{
var imageStream = new MemoryStream();
img.Save(imageStream);
img.SaveAsPng(imageStream);
imageStream.Position = 0;
return imageStream;
}
@ -198,11 +200,11 @@ namespace NadekoBot.Extensions
return await ownerPrivate.SendMessageAsync(message).ConfigureAwait(false);
}
public static ImageSharp.Image Merge(this IEnumerable<ImageSharp.Image> images)
public static Image<Rgba32> Merge(this IEnumerable<ImageSharp.Image<Rgba32>> images)
{
var imgs = images.ToArray();
var canvas = new ImageSharp.Image(imgs.Sum(img => img.Width), imgs.Max(img => img.Height));
var canvas = new Image<Rgba32>(imgs.Sum(img => img.Width), imgs.Max(img => img.Height));
var xOffset = 0;
for (int i = 0; i < imgs.Length; i++)