【发布时间】:2025-11-30 15:45:01
【问题描述】:
我想在 foreach 语句中使用相同图像的流。问题是在 foreach 语句中处理了第一项之后,“图像”字典中的流被清空(长度 = 0)。
我唯一的选择是将流复制到文件或数据库吗?如果可能的话,我想避免这种情况。
这是我的代码:
public int SendImage(string TweetText, List<long> TweetIDs, Dictionary<string, Stream> images)
{
TwitterService service = twitterAPI.Twitterservice();
GetTweetOptions tweetOptions = new GetTweetOptions();
//It works fine on the first tweetid but after that the Stream's length = 0
foreach (long _tweetid in TweetIDs)
{
tweetOptions.Id = _tweetid;
TwitterUser twitteruser = service.GetTweet(tweetOptions).User;
service.SendTweetWithMedia(new SendTweetWithMediaOptions { Status = "@" + twitteruser.ScreenName + " " + TweetText, InReplyToStatusId = _tweetid, Images = images });
}
}
谢谢!
【问题讨论】:
-
您可以将其复制到
MemoryStream。这不会解决它被处置的问题,但如果需要,它可以让您以byte[]的形式访问数据。 -
根据 for each 循环内的图像创建一个新变量,以便在调用 SendTweetWithMedia 后它不会被您的进程使用。不要使用 var newVar = Oliver,而是使用序列化/反序列化方式
-
您的流是否可搜索?如果是这样,您可以将其重置回其起始位置。
-
感谢您的回复。您能告诉我将 Seek 行放在代码中的什么位置吗?
-
Jawad -- 在 SendTweetWithMedia 行之后消费流。
标签: c# image dictionary stream tweetsharp