0

I'm having severe trouble with this. I have a function that asks for a stream that is read and returned by an additional function. The stream belongs to a PNG-image that is portrayed as a backgroundimage in a picturebox on the form. This works out fine, but I want to save the stream data in the class streamInfo in the variable streamData so that I can later pass it to another function that enables me to upload the stream or rather the picture to a different location of the FTP server. When I pass the stream as an argument I receive the error

Stream not readable

If I try to output the stream data in the console it just says System.Net.FtpdataStream.

How can I save the returned Stream (preferablly in the class and without converting or encoding it) and later pass it to the other function so that it is still readable and can be passed as an upload argument?

This is the main funtion:

                if (content.Contains(box.SelectedItem.ToString()))
                {                      
                    stream = request_Preview_Image("somePath" + content);
                    this.imagePreview.BackgroundImage = Image.FromStream(stream);
                    blitzStream.streamData = stream;
                    blitzStream.streamName = content;
                }

this is the function that's called:

        Stream request_Preview_Image(string create)
        {
            FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create("ftp://someFTP" + create);
            ftp.Credentials = someCredentials;
            ftp.Method = WebRequestMethods.Ftp.DownloadFile;
            FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
            try
            {
                return (Stream)response.GetResponseStream();
            }
            catch
            {
                return (Stream)null;
            }
        }

This is the class:

    private class streamInfo
    {
        public string streamName { get; set; } = null;
        public Stream streamData { get; set; } 
    }

Many thanks!

d219
  • 2,707
  • 5
  • 31
  • 36
  • 1
    Exception handling is a pet peeve of mine and yours unfortunately cecks all the bad marks. Unless I am missing soemthing, taht try...catch will swallow every Exception. And swallowing exceptions is a deadly sin of exception handling. Here are two articles on the mater I do link often: https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/ | https://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET – Christopher Oct 22 '18 at 18:47
  • 2
    An http response stream is not seekable. What this means is that once you've read the stream, you cannot seek back to the start so it can be read again. You need to first read the stream into a seekable stream (like a MemoryStream) before doing anything else with it. Then once something has read it, you can seek back to origin and it can be read again. – Kevin Oct 22 '18 at 19:36

1 Answers1

1

If you are trying to store the contents of the stream for later retrieval in stream form, you can use this: How do I copy the contents of one stream to another?. It sounds like you're creating a reference to that stream, but somewhere between creating the reference and trying to save it, you are using the stream to, for example, read the contents, and maybe close it. But having streams of images sitting in memory like this for a web server is bad practice. Your site cannot scale with this approach.

d219
  • 2,707
  • 5
  • 31
  • 36
Lev
  • 74
  • 5