Search code examples
c#tweetsharp

Tweetsharp SendTweetWithMedia from url


I am trying to use Tweetsharp's SendTweetWithMedia with a image which I don't have stored locally, I only have a url. All the examples I have found of SendTweetWithMedia use a file on the local system.

var thumb = "http://somesite.net/imageurl";
var service = new TwitterService(key, secret);
service.AuthenticateWith(token, tokenSecret);
var req = WebRequest.Create(thumb);
using (var stream = req.GetResponse().GetResponseStream())
{
    response = service.SendTweetWithMedia(new SendTweetWithMediaOptions
    {
    Status = tweet.Trim(),
    Images = new Dictionary<string, Stream> { { fullname, stream } }
    });
} 

I get the following error from SendTweetWithMedia:

'System.NotSupportedException': This stream does not support seek operations.

I could download the file from the url and save locally, but I'd rather use the url. Is this possible?


Solution

  • In the end, I just created a temporary file:

    byte[] data;
    using (var client = new WebClient())
    {
        data = client.DownloadData(thumb);
    }
    File.WriteAllBytes($"{Path.GetTempPath()}\\xyz.jpg", data);
    

    best answer I could come up with. Still a few more lines than I'd like though.