Search code examples
c#imagestreammemorystream

Unable to create System.Drawing.Image object from WebResponse Stream


I am trying to create a System.Drawing.Image object from a WebResponse's response stream. Here's what I'm doing:

using (WebResponse response = await request.GetResponseAsync())
{
    using (Stream originalInputStream = response.GetResponseStream())
    {
         // some code that calls a 3rd party image resizer, passing in the original stream
         ResizeImage(originalInputStream, out resizedOutputStream);

         // manipulation of the resizedOutputStream

         // now i want to create an image from the ORIGINAL stream 
         // ERROR HAPPENS HERE!!
         using (Image image = Image.FromStream(originalInputStream))
         {
              // random code that doesn't get hit because of the error above
         }
    }
}

When the program tries to create the Image.FromStream() in the using statement, I get an error saying:

'Parameter is not valid.'

I assume this is because of the manipulation of the originalInputStream in the resizer function. I searched around and found that resetting the position of the stream can solve these issues. So I tried that, using both:

originalInputStream.Seek(0, SeekOrigin.Begin);
originalInputStream.Position = 0;

but both of those are erroring out as well, giving me an error message of:

Specified method is not supported.

When I try and create the Image.FromStream(), WITHOUT any of the preceding image resizing/stream manipulation... IT WORKS. But THEN, I cannot do anything else with the stream afterwards, or else it will error out as before. However, I need to do the manipulation, and also create the Image, so I'm stuck.

Is the best course of action to simply create another request? And just create the image from the new request's response stream? that just seems like bad practice, I don't know, I feel like what I'm doing should theoretically work and I'm probably just doing something dumb.

Thanks in advance for the help, let me know if I can provide any extra info.


Solution

  • Resetting the stream position does not work on the Stream base class. If you can read the response into a MemoryStream, then you will be able to manipulate the image as you please. Something like this could work:

    using (WebResponse response = await request.GetResponseAsync())
    {
        using (Stream responseStream = response.GetResponseStream())
        {
            using (MemoryStream memStream = new MemoryStream()) 
            {
                responseStream.CopyTo(memStream);
                // some code that calls a 3rd party image resizer, passing in the original stream
                ResizeImage(memStream, out resizedOutputStream);
                memStream.Seek(0, SeekOrigin.Begin);
    
                // manipulation of the resizedOutputStream
    
                // now i want to create an image from the ORIGINAL stream 
                using (Image image = Image.FromStream(memStream))
                {
    
                }
            }
        }
    }