简体   繁体   English

如何正确使用流通过 discordbot 检索、编辑、保存和发送图像

[英]How to correctly use streams to retrieve, edit, save and send image via discordbot

I am writing a discord bot using DSharp plus library.我正在使用 DSharp plus 库编写一个 discord 机器人。

The command I am currently writing gets an image from the discord chat I then edit the image and send the edited image back as a single frame gif.我目前正在编写的命令从 discord 聊天中获取图像,然后我编辑图像并将编辑后的图像作为单帧 gif 发回。

I retrieve the image from the image url by using:我使用以下方法从图像 url 中检索图像:

HttpClient client = new HttpClient();
Stream stream = await client.GetStreamAsync(attachments[0].Url);
Bitmap image = new Bitmap(System.Drawing.Image.FromStream(stream));

I then call my edit function and edit the image.然后我打电话给我的编辑 function 并编辑图像。 I then save the image to my files using:然后我使用以下方法将图像保存到我的文件中:

using (var stream = new FileStream("Images/output.gif", FileMode.Create))
{
   imgToGif.SaveAsGif(stream);
}

where .SaveAsGif() is a function from the KGySoft.Drawing library I found online.其中.SaveAsGif()是我在网上找到的 KGySoft.Drawing 库中的 function。

To send the edited image back I use:要将编辑后的图像发回,我使用:

FileStream file = new FileStream("Images/output.gif", FileMode.Open);

DiscordMessageBuilder messagefile = new DiscordMessageBuilder();

messagefile.AddFile(file);

ctx.RespondAsync(messagefile);

But this throws a "The process cannot access the file "Image/output.gif" because it is being used by another process."但这会引发“该进程无法访问文件“Image/output.gif”,因为它正被另一个进程使用。” error.错误。

After some googling I tried to close the FileStream which saves my image to my files using stream.close() or stream.dispose() .经过一番谷歌搜索后,我尝试使用stream.close() or stream.dispose()关闭将图像保存到文件中的 FileStream。 The problem however is that I cannot acces the stream again because it will throw the "Cannot acces closed stream error".然而,问题是我无法再次访问 stream,因为它会抛出“无法访问关闭 stream 错误”。

I also tried using FileShare.read, FileShare.ReadWrite .我也尝试使用FileShare.read, FileShare.ReadWrite

Tried closing both stream and tried to use 1 stream only.尝试同时关闭 stream 并尝试仅使用 1 stream。 So I kept the stream open and used it to send the message in discord chat but that would send a file with 0 bytes in the discord chat.所以我让 stream 保持打开状态,并用它在 discord 聊天中发送消息,但这会在 discord 聊天中发送一个 0 字节的文件。

I think you closed the stream too early while sending the gif我认为您在发送 gif 时过早关闭了 stream

you need to call file.Close() after you call RespondAsync() and you need to change ctx.RespondAsync(messagefile);您需要在调用file.Close()之后调用 file.Close( RespondAsync()并且您需要更改ctx.RespondAsync(messagefile); to await ctx.RespondAsync(messagefile); await ctx.RespondAsync(messagefile); because RespondAsync() is an asynchronous method if you dont use await rest of the code will continue running so the stream will close while ctx.RespondAsync(messagefile);因为RespondAsync()是一种异步方法,如果您不使用await rest 代码将继续运行,因此 stream 将关闭,而ctx.RespondAsync(messagefile); is still running and it will give an error.仍在运行,它会报错。

sending a gif part should look like this:发送 gif 部分应如下所示:

FileStream file = new FileStream("Images/output.gif", FileMode.Open);
DiscordMessageBuilder messagefile = new DiscordMessageBuilder();
messagefile.AddFile(file);
await ctx.RespondAsync(messagefile);
file.Close();

if you have done the rest of the code correct this should work.如果你已经完成了 rest 的代码正确这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM