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());
}
On Tue, 24 Oct 2006 21:56:58 +0200, <rh******@gmail.comwrote:
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.
Jon wrote:
><rh******@gmail.comwrote:
Hey All. I'm trying to download an image from a URL and display itin
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.GetResponseStr eam();
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.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
--
Happy Coding!
Morten Wennevik [C# MVP]