C# 电报机器人下载图像文件

C# 电报机器人下载图像文件,c#,bots,telegram,telegram-bot,C#,Bots,Telegram,Telegram Bot,我正试图用我的机器人下载文件(图像),但当我在使用getFile后下载图像(成功完成)时,我收到的图像非常小,只有1.7KB,而它比我手机上的图像大 getFile方法提供一个JSON对象(1.7KB响应),其中包含用于访问图像文件的数据 还要注意,电报为任何图像创建一个图像数组。此数组的第一个元素包含原始图像的小缩略图,数组的最新元素包含原始图像 这是一个老帖子。但由于没有关于如何在telegram bot中下载文件的好文档,对于任何想知道的人来说,这就是您应该如何做的(一种方法): 其中:

我正试图用我的机器人下载文件(图像),但当我在使用getFile后下载图像(成功完成)时,我收到的图像非常小,只有1.7KB,而它比我手机上的图像大

  • getFile
    方法提供一个JSON对象(1.7KB响应),其中包含用于访问图像文件的数据

  • 还要注意,电报为任何图像创建一个图像数组。此数组的第一个元素包含原始图像的小缩略图,数组的最新元素包含原始图像


  • 这是一个老帖子。但由于没有关于如何在telegram bot中下载文件的好文档,对于任何想知道的人来说,这就是您应该如何做的(一种方法):

    其中:

        private static async void DownloadFile(string fileId, string path)
        {
            try
            {
                var file = await Bot.GetFileAsync(fileId);
    
                using (var saveImageStream = new FileStream(path, FileMode.Create))
                {
                    await file.FileStream.CopyToAsync(saveImageStream);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error downloading: " + ex.Message);
            }
        }
    

    message.Photo[message.Photo.Length-1]
    message.Photo
    数组中的最后一个元素,它包含最高质量的图像数据。显然,您也可以使用
    DownloadFile
    下载其他类型的文件(例如
    message.Document

    我使用telegram.bot v14.10.0,但我找不到file.FileStream,所以我找到了从telegram获取图像的替代方法。我的方法是在这种情况下直接使用电报api

                        var test =  _myBot.GetFileAsync(e.Message.Photo[e.Message.Photo.Count() - 1].FileId);
                        var download_url = @"https://api.telegram.org/file/bot<token>/" + test.Result.FilePath;
                        using (WebClient client = new WebClient())
                        {
                            client.DownloadFile(new Uri(download_url), @"c:\temp\NewCompanyPicure.png");
                        }
                        //then do what you want with it
    
    var test=\u myBot.GetFileAsync(e.Message.Photo[e.Message.Photo.Count()-1].FileId);
    var下载\u url=@“https://api.telegram.org/file/bot/“+test.Result.FilePath;
    使用(WebClient=newWebClient())
    {
    client.DownloadFile(新Uri(下载url),@“c:\temp\NewCompanyPicure.png”);
    }
    //那你想怎么做就怎么做
    
    你能给我们看看你的一些作品吗?同样供您阅读:请描述一些工作流程或评论您的代码。
                        var test =  _myBot.GetFileAsync(e.Message.Photo[e.Message.Photo.Count() - 1].FileId);
                        var download_url = @"https://api.telegram.org/file/bot<token>/" + test.Result.FilePath;
                        using (WebClient client = new WebClient())
                        {
                            client.DownloadFile(new Uri(download_url), @"c:\temp\NewCompanyPicure.png");
                        }
                        //then do what you want with it
    
    var file = await Bot.GetFileAsync(message.Document.FileId);
    FileStream fs=new FileStream("Your Destination Path And File Name",FileMode.Create);
    await Bot.DownloadFileAsync(file.FilePath, fs);
    fs.Close();
    fs.Dispose();