电报机器人下载图像文件 [英] Telegram Bot download image file

查看:86
本文介绍了电报机器人下载图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的机器人下载文件(图像),但是当我使用getFile下载图像(成功完成)后,收到的图像很小1.7 kb,但比移动设备上的图像大电话

I'm trying to download file (image ) using my bot, but when I download the image ( which is done successfully ) after using getFile, the image I received is very small 1.7 kb while it's bigger than that on my mobile phone

推荐答案

这是旧文章.但是,由于没有足够的文档说明如何在电报bot中下载文件,因此任何人都想知道,这就是应该怎么做(一种方法):

This is an old post. But since there is not a good documentation on how you should download file in telegram bot, for anyone wondering, that's how you should do it(One way of it):

DownloadFile(message.Photo[message.Photo.Length - 1].FileId, @"c:\photo.jpg");

其中:

    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).

The message.Photo[message.Photo.Length - 1] is the last element in message.Photo array, which contains the highest quality image data. Obviously you can use DownloadFile to download other kind of files(for example message.Document) as well.

这篇关于电报机器人下载图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆