public async Task <Stream> WriteToStream(IImageWrapper img) { MemoryStream stream = new MemoryStream(); await Task.Run(() => img.Write(stream)); stream.Position = 0; return(stream); }
public async Task ImageCommand(params string[] args) { string url = args.Last(); if (url.Contains("http")) { Array.Resize(ref args, args.Length - 1); //remove the last element } else if (Context.Message.Attachments.Count == 1) { url = Context.Message.Attachments.First().Url; } else { var messages = await Context.Channel.GetMessagesAsync(20).FlattenAsync(); var lastAttachments = messages.Where(x => x.Attachments.Count == 1); if (!lastAttachments.Any()) { await ReplyAsync("`No images found in the last 20 messages.`"); return; } url = lastAttachments.First().Attachments.First().Url; } var typing = Context.Channel.EnterTypingState(); IImageWrapper img = await ProcessImageAsync(url, args); using (var stream = new MemoryStream()) { img.Write(stream); var im = new MagickImage(); stream.Position = 0; await Context.Channel.SendFileAsync(stream, "lossyimage." + img.GetFormat().ToString().ToLower()); } typing.Dispose(); }