-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathImageExtensions.cs
74 lines (68 loc) · 2.71 KB
/
ImageExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using IronSoftware.Drawing;
using SixLabors.ImageSharp;
using System.IO;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
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<Stream> ToStream(this SKImage image)
{
var stream = new MemoryStream();
await image.SaveAsPngAsync(stream);
stream.Position = 0;
return stream;
}
/// <summary>
/// Sends an image
/// </summary>
/// <param name="image"></param>
/// <param name="name"></param>
/// <param name="buttons"></param>
/// <param name="replyTo"></param>
/// <param name="disableNotification"></param>
/// <returns></returns>
public static async Task<Message> SendPhoto(this IDeviceSession 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);
}
}
/// <summary>
/// Sends an image
/// </summary>
/// <param name="image"></param>
/// <param name="name"></param>
/// <param name="buttons"></param>
/// <param name="replyTo"></param>
/// <param name="disableNotification"></param>
/// <returns></returns>
public static async Task<Message> SendPhoto(this IDeviceSession 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);
}
}
}
}