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.Memor yStream MemoryStream =
(System.IO.Memo ryStream)WebRes ponse.GetRespon seStream();
Here's my code:
Response.Clear( );
Response.Conten tType = "image/gif";
System.Net.WebR equest ImageWebRequest ;
ImageWebRequest = System.Net.WebR equest.Create(i mgURI);
System.Net.WebR esponse WebResponse = ImageWebRequest .GetResponse();
System.IO.Memor yStream MemoryStream =
(System.IO.Memo ryStream)WebRes ponse.GetRespon seStream();
MemoryStream.Ge tBuffer();
Response.Binary Write(MemoryStr eam.GetBuffer() );
MemoryStream.Cl ose();
Any ideas? Thanks. 6 20906
<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.Memor yStream MemoryStream =
(System.IO.Memo ryStream)WebRes ponse.GetRespon seStream();
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.Ge tBuffer() 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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.Memor yStream MemoryStream =
(System.IO.Memo ryStream)WebRes ponse.GetRespon seStream();
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.Ge tBuffer() 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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.Memor yStream MemoryStream =
(System.IO.Memo ryStream)WebRes ponse.GetRespon seStream();
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.Ge tBuffer() 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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.Conten tType = "image/gif";
WebRequest ImageWebRequest ;
ImageWebRequest = WebRequest.Crea te(imgURL");
WebResponse WebResponse = ImageWebRequest .GetResponse();
Stream s = WebResponse.Get ResponseStream( );
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.Binary Write(ms.ToArra y());
}
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******@gmai l.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.Memor yStream MemoryStream =
(System.IO.Memo ryStream)WebRes ponse.GetRespon seStream();
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.Ge tBuffer() 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.co m> 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]
<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.Get ResponseStream( ) 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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.Conten tType = "image/gif";
WebRequest ImageWebRequest ;
ImageWebRequest = WebRequest.Crea te(imgURL");
WebResponse WebResponse = ImageWebRequest .GetResponse();
Stream s = WebResponse.Get ResponseStream( );
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.Binary Write(ms.ToArra y());
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 from the original lenght in order to get it to
work
as there seems to be 2 extra 0 bytes at the end.
Functions included
Stu
|
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 line at a time, my code can be
quite slow.
Is it possible, to use a memory stream, such that I read the entire
file in to memory first, and then can use it, just like a file stream?
Similarly, can I write to memory, just like I would be writing...
|
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 a new memory stream, filling it with
the encrypted bytes then trying to read from the crypto stream object. I
have used the same approach encrypting to a file handle and then decrypting
from the same file and it works.
|
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 = im.FromStream(stream)
im = Resize(im, 16, 16)
stream.Position = 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 optional, yet when I look in the object browser
it tells me that for the overloaded function I am using that I only need two
parameters for this particular function. If you have any help with this or
help on how to create an API comaptiple pointer to a...
| |
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 results .. it should do
My question is this. Is the WriteTo() method the proper one to use to
pass the contents of my memory stream into the
|
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 New Hashtable
H.Add(1, "First Item")
H.Add(2, "Second Item")
H.Add(3, "Third Item")
Dim BF As New
|
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 correct way to get it into a memory
stream.
Any help appreciated.
|
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 again in to word
object. If some one has some brilliant idea please share.
In a nutshell this is what I'm currently doing
ByteArray --File Stream --Word Object
and this I want to do
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |