From 9927a034b0f8f0135c0d907ea1c5c17a2e6391c0 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Thu, 4 Jan 2024 13:44:18 +0100 Subject: [PATCH] First release of IronSoftware drawing extension --- .../ImageExtensions.cs | 73 +++++++++++++++++++ .../README.md | 7 ++ ...Base.Extensions.Images.IronSoftware.csproj | 25 +++++++ TelegramBotFramework.sln | 7 ++ 4 files changed, 112 insertions(+) create mode 100644 TelegramBotBase.Extensions.Images.IronSoftware/ImageExtensions.cs create mode 100644 TelegramBotBase.Extensions.Images.IronSoftware/README.md create mode 100644 TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj diff --git a/TelegramBotBase.Extensions.Images.IronSoftware/ImageExtensions.cs b/TelegramBotBase.Extensions.Images.IronSoftware/ImageExtensions.cs new file mode 100644 index 0000000..bc067ee --- /dev/null +++ b/TelegramBotBase.Extensions.Images.IronSoftware/ImageExtensions.cs @@ -0,0 +1,73 @@ +using IronSoftware.Drawing; +using SixLabors.ImageSharp; +using System.IO; +using System.Threading.Tasks; +using Telegram.Bot.Types; +using TelegramBotBase.Form; +using TelegramBotBase.Sessions; +using static IronSoftware.Drawing.AnyBitmap; +using SKImage = SixLabors.ImageSharp.Image; + +namespace TelegramBotBase.Extensions.Images.IronSoftware +{ + public static class ImageExtensions + { + public static Stream ToStream(this AnyBitmap image, ImageFormat format) + { + var stream = new MemoryStream(); + image.ExportStream(stream, format); + stream.Position = 0; + return stream; + } + + public static async Task ToStream(this SKImage image) + { + var stream = new MemoryStream(); + await image.SaveAsPngAsync(stream); + stream.Position = 0; + return stream; + } + + /// + /// Sends an image + /// + /// + /// + /// + /// + /// + /// + public static async Task SendPhoto(this DeviceSession session, AnyBitmap image, string name, + string caption, ButtonForm buttons = null, int replyTo = 0, + bool disableNotification = false) + { + using (var fileStream = ToStream(image, ImageFormat.Png)) + { + var fts = InputFile.FromStream(fileStream, name); + + return await session.SendPhoto(fts, caption, buttons, replyTo, disableNotification); + } + } + + /// + /// Sends an image + /// + /// + /// + /// + /// + /// + /// + public static async Task SendPhoto(this DeviceSession session, SKImage image, string name, + string caption, ButtonForm buttons = null, int replyTo = 0, + bool disableNotification = false) + { + using (var fileStream = await ToStream(image)) + { + var fts = InputFile.FromStream(fileStream, name); + + return await session.SendPhoto(fts, caption, buttons, replyTo, disableNotification); + } + } + } +} \ No newline at end of file diff --git a/TelegramBotBase.Extensions.Images.IronSoftware/README.md b/TelegramBotBase.Extensions.Images.IronSoftware/README.md new file mode 100644 index 0000000..879e552 --- /dev/null +++ b/TelegramBotBase.Extensions.Images.IronSoftware/README.md @@ -0,0 +1,7 @@ +# TelegramBotBase.Extensions.Images.IronSoftware + +[![NuGet version (TelegramBotBase)](https://img.shields.io/nuget/v/TelegramBotBase.Extensions.Images.IronSoftware.svg?style=flat-square)](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/) +[![Telegram chat](https://img.shields.io/badge/Support_Chat-Telegram-blue.svg?style=flat-square)](https://www.t.me/tgbotbase) + +[![License](https://img.shields.io/github/license/MajMcCloud/telegrambotframework.svg?style=flat-square&maxAge=2592000&label=License)](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md) +[![Package Downloads](https://img.shields.io/nuget/dt/TelegramBotBase.Extensions.Images.IronSoftware.svg?style=flat-square&label=Package%20Downloads)](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware) diff --git a/TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj b/TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj new file mode 100644 index 0000000..0e60a55 --- /dev/null +++ b/TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj @@ -0,0 +1,25 @@ + + + + netstandard2.0;netcoreapp3.1;net6 + https://github.com/MajMcCloud/TelegramBotFramework + https://github.com/MajMcCloud/TelegramBotFramework + MIT + true + snupkg + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/TelegramBotFramework.sln b/TelegramBotFramework.sln index 4d81707..409d950 100644 --- a/TelegramBotFramework.sln +++ b/TelegramBotFramework.sln @@ -36,6 +36,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DependencyInjection", "Exam EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelegramBotBase.Extensions.Serializer.Database.PostgreSql", "TelegramBotBase.Extensions.Serializer.Database.PostgreSql\TelegramBotBase.Extensions.Serializer.Database.PostgreSql.csproj", "{7C55D9FF-7DC1-41D0-809C-469EBFA20992}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelegramBotBase.Extensions.Images.IronSoftware", "TelegramBotBase.Extensions.Images.IronSoftware\TelegramBotBase.Extensions.Images.IronSoftware.csproj", "{DC521A4C-7446-46F7-845B-AAF10EDCF8C6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -90,6 +92,10 @@ Global {7C55D9FF-7DC1-41D0-809C-469EBFA20992}.Debug|Any CPU.Build.0 = Debug|Any CPU {7C55D9FF-7DC1-41D0-809C-469EBFA20992}.Release|Any CPU.ActiveCfg = Release|Any CPU {7C55D9FF-7DC1-41D0-809C-469EBFA20992}.Release|Any CPU.Build.0 = Release|Any CPU + {DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -105,6 +111,7 @@ Global {067E8EBE-F90A-4AFF-A0FF-20578216486E} = {BFA71E3F-31C0-4FC1-A320-4DCF704768C5} {689B16BC-200E-4C68-BB2E-8B209070849B} = {BFA71E3F-31C0-4FC1-A320-4DCF704768C5} {7C55D9FF-7DC1-41D0-809C-469EBFA20992} = {E3193182-6FDA-4FA3-AD26-A487291E7681} + {DC521A4C-7446-46F7-845B-AAF10EDCF8C6} = {E3193182-6FDA-4FA3-AD26-A487291E7681} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {59CB40E1-9FA7-4867-A56F-4F418286F057}