Persist Stream in foreach statement in C#?

737 Views Asked by At

I would like to use a stream of the same image in a foreach statement. The problem is that after the first item is processed in the foreach statement the stream in the "images" dictionary is emptied (length = 0).

Are my only options are to copy the stream to a file or database? I would like to avoid that if it's possible.

Here is my code:

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

                }
          }

Thank you!

2

There are 2 best solutions below

0
Clark Bohs On BEST ANSWER

Thanks to [John_ReinstateMonica][1] for helping me solve the problem.

Rather than passing the image stream I'm passing the byte object of that stream then included it in the foreach block then converted it to a stream for every iteration.

Now the image is being sent for each tweet!

//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://stackoverflow.com/users/3181933/john-reinstatemonica
1
Aydin On

If the stream hasn't been disposed, you can set the position of the stream back to the beginning...

stream.Seek(0, SeekOrigin.Begin);