473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Stream To Memory Stream Error

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.GetResponseStr eam();

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.GetResponseStr eam();

MemoryStream.GetBuffer();
Response.BinaryWrite(MemoryStream.GetBuffer());
MemoryStream.Close();
Any ideas? Thanks.

Oct 24 '06 #1
6 20897
<rh******@gmail.comwrote:
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.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
Oct 24 '06 #2
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 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.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
Oct 24 '06 #3
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 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.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
Oct 24 '06 #4
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]
Oct 25 '06 #5
<rh******@gmail.comwrote:
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.

--
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
Oct 25 '06 #6
Great that worked. Thanks for your help guys.
Morten Wennevik wrote:
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());
}
Oct 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stu | last post by:
Hi, Im reading a file in from disk as a byte array then passing it to a memory stream for decryption using crypto api functions. What I have found is that you need to reduce the array length by 2...
2
by: andrew | last post by:
I am writing a program that involves reading in lots of (ASCII) data, manipulating it, and then outputting some more. I am using ifstream and ofstream. Since I am reading a line, and writing a...
3
by: JW | last post by:
I can encrypt the contents of a memory stream with no problems, however when decrypting( I'm using the same key an IV ) I get an exception stating bad data. I'm at a lost. I'm actually creating...
0
by: Dennis | last post by:
I have the following: Dim stream As New IO.MemoryStream fmParent.Icon = icon.FromHandle(New IntPtr(retval)) frmParent.Icon.Save(stream) 'RESIZE ICON to 16 x 16 Dim im As Image im =...
0
by: Christopher Kurtis Koeber | last post by:
Dear All, I am having problems saving an image to a memory stream to use it in an API function. The error occurs in the save method of the bitmap. It tells me that the arguement cannot be...
0
by: student | last post by:
I have an object created called objM_message. I want to serialize it as xml and place the xml in my HttpWebRequest.GetRequestStream. But the line resp = req.GetResponse doesnt return any...
1
by: Zorpiedoman | last post by:
The following code explains my problem. It works fine if I use a file stream, but not if I use a memory stream. What's the problem here? It must be something simple I am missing... Dim H As...
14
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the...
5
by: Nitin Mahajan | last post by:
Guys Is there a way in C# to create a word object directly from a memory stream without passing that to hard disk (file stream). I think it doesn't makes sense to create a file just to read it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.