Stream To Memory Stream Error

R

rh1200la

Hey All. I'm trying to download an image from a URL and display it in
my page. I'm getting an error converting the Stream to Memory Stream.


I'm getting a Specified cast is not valid error for the line:
System.IO.MemoryStream MemoryStream =
(System.IO.MemoryStream)WebResponse.GetResponseStream();

Here's my code:

Response.Clear();
Response.ContentType = "image/gif";
System.Net.WebRequest ImageWebRequest;

ImageWebRequest = System.Net.WebRequest.Create(imgURI);

System.Net.WebResponse WebResponse = ImageWebRequest.GetResponse();
System.IO.MemoryStream MemoryStream =
(System.IO.MemoryStream)WebResponse.GetResponseStream();

MemoryStream.GetBuffer();
Response.BinaryWrite(MemoryStream.GetBuffer());
MemoryStream.Close();


Any ideas? Thanks.
 
J

Jon Skeet [C# MVP]

Hey All. I'm trying to download an image from a URL and display it in
my page. I'm getting an error converting the Stream to Memory Stream.


I'm getting a Specified cast is not valid error for the line:
System.IO.MemoryStream MemoryStream =
(System.IO.MemoryStream)WebResponse.GetResponseStream();

Well, yes - because it's *not* a MemoryStream. Why did you expect it to
work, and why do you think you need to use a MemoryStream?

Note that MemoryStream.GetBuffer() isn't what you want anyway -
ToArray() is, otherwise you can end up with extra bytes at the end.

Also note that naming local variables the same as the type (including
case) is pretty confusing - it makes it look like you're calling static
methods when you're actually calling instance methods.
 
R

rhungund

I got the following code from a few sources online and this was the way
they performed the requested function.

Do you have an idea on the proper way to get it to work? Most examples
found online were how to display images from a database. Thanks.
 
R

rh1200la

I got the following code from a few sources online and this was the way
they performed the requested function.

Do you have an idea on the proper way to get it to work? Most examples
found online were how to display images from a database. Thanks.
 
M

Morten Wennevik

Hi,

You need to read from the response stream and write it to a MemoryStream,
once complete, feed the MemoryStream to the response.

Rewriting your code it can be done like this

using System.Net;
using System.IO;

....

Response.Clear();
Response.ContentType = "image/gif";
WebRequest ImageWebRequest;

ImageWebRequest = WebRequest.Create(imgURL");

WebResponse WebResponse = ImageWebRequest.GetResponse();
Stream s = WebResponse.GetResponseStream();
using (MemoryStream ms = new MemoryStream())
{
int bytes = 0;
byte[] temp = new byte[4096];
while ((bytes = s.Read(temp, 0, temp.Length)) > 0)
ms.Write(temp, 0, bytes);
Response.BinaryWrite(ms.ToArray());
}




I got the following code from a few sources online and this was the way
they performed the requested function.

Do you have an idea on the proper way to get it to work? Most examples
found online were how to display images from a database. Thanks.
 
J

Jon Skeet [C# MVP]

I got the following code from a few sources online and this was the way
they performed the requested function.

Anything which casts WebResponse.GetResponseStream() to a MemoryStream
is just asking for trouble.
Do you have an idea on the proper way to get it to work? Most examples
found online were how to display images from a database. Thanks.

See http://www.pobox.com/~skeet/csharp/readbinary.html for some code to
read the whole of a stream into a byte array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top