2017-07-03 51 views
-1

所以我试图在C#中使用VB.NET 2012中的TweetSharp发布带图片的推文。如何将SendTweetWithMedia与TweetSharp一起使用?

我发现了如何做到这一点的代码示例:

service.SendTweetWithMedia(new SendTweetWithMediaOptions 
{ 
    Status = "message", 
    Images = dictionary 
} 
); 

但是我不知道如何创建“字典”与画面流。

我尝试这样做:

Dictionary<string, Stream> imageDict = new Dictionary<string, Stream>(); 

那么以后引用:

Images = imageDict 

但它给人的错误:

Error Screenshot

人有怎样,这是任何想法应该工作?

另一个代码块,我发现并试图为:

using (var stream = new FileStream("Image.jpg", FileMode.Open)) 
    { 
     var result = tservice.SendTweetWithMedia(new SendTweetWithMediaOptions 
     { 
      Status = "Message", 
      Images = new Dictionary<string, Stream> { { "john", stream } } 
     }); 
     lblResult.Text = result.Text.ToString(); 
    } 

但它提供了有关“的FileStream”同样的错误。

回答

0

您需要添加对System.IO命名空间的引用,这就是为什么您在发布的映像中收到此错误的原因。

下面是一个例子:分享Tweet创造哪怕是within the file size limit期间

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 } } 
    }); 
} 

一个GIF可能会失败。坚持以下约束来提高成功率。

  1. 分辨率应< = 1280x1080(宽x高)
  2. 帧数< = 350
  3. 像素(宽×高×num_frames)数量< = 3亿
  4. 作品尺寸< = 15兆字节
+0

我完全错过了!非常感谢。现在我得到另一个错误。它现在完美发布图像,但如果图像很大,“结果”会以Null(发生错误)的形式出现,则会引发NullRefrenceException。我怎么能错误检查这个?我尝试了一个Try/Catch块,但它没有奏效。 –

相关问题