在 C# 中的 foreach 语句中保留流?

Persist Stream in foreach statement in C#?

我想在 foreach 语句中使用相同图像的流。问题是在 foreach 语句中处理第一项后,"images" 字典中的流被清空(长度 = 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 });                       

                }
          }

谢谢!

如果流还没有被释放,你可以将流的位置设置回开头...

stream.Seek(0, SeekOrigin.Begin);

感谢[John_ReinstateMonica][1]帮我解决问题。

我没有传递图像流,而是传递该流的字节对象,然后将其包含在 foreach 块中,然后在每次迭代时将其转换为流。

现在正在为每条推文发送图片!

//This is the method that calls the SendImage method
//Note that the file is converted to a byte object
//then that will be sent to the SendImage method
[HttpPost]
        public ActionResult TweetImage(string TweetText)

        {
            var file = Request.Files[0];            

            //// Read bytes from http input stream
           BinaryReader b = new BinaryReader(file.InputStream);
          byte[] binData = b.ReadBytes(file.ContentLength);

            var tweetIDs = (List<long>)TempData["TweetIDList"];

            tweetsend.SendImage(TweetText, tweetIDs, binData);

            return View("SendTweetsByString");
        }

     public int SendImage(string TweetText, List<long> TweetIDs, byte[] binData)  
            {
                //I'm passing the byte object "binData" to be 
                //converted back to an image stream in the foreach block

                TwitterService service = twitterAPI.Twitterservice();

                GetTweetOptions tweetOptions = new GetTweetOptions();

                try
                {
                    foreach (long _tweetid in TweetIDs)
                    {
                        tweetOptions.Id = _tweetid;

                        TwitterUser twitteruser = service.GetTweet(tweetOptions).User;

                        try
                        {    

                           Stream st = new MemoryStream(binData);

                            Dictionary<string, Stream> _images = new Dictionary<string, Stream> { { "mypicture", st } };

                            service.SendTweetWithMedia(new SendTweetWithMediaOptions { Status = "@" + twitteruser.ScreenName + " " + TweetText, InReplyToStatusId = _tweetid, Images = _images });

                        }
                        catch (Exception ex)
                        {

                            return 1;
                        }
                    }

                }
                catch (Exception ex)
                {
                    return 1;

                }

                return 0;

            }


-------------------------------------------------------------------------------------

You could copy it to a MemoryStream. This won't solve the problem with it being disposed, but it gives you access to the data as a byte[] if needs be. – John_ReinstateMonica Dec 16 at 2:35 


  [1]: https://whosebug.com/users/3181933/john-reinstatemonica