Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I want to return image as image type using its URL.
I found this function but it does not return an image .

WebResponse response;
Stream RemoteStream;
StreamReader SR;
WebRequest request = WebRequest.Create(URL);
response = request.GetResponse();
RemoteStream = response.GetResponseStream();
SR = new StreamReader(RemoteStream);
System.Drawing.Image img = System.Drawing.Image.FromStream(RemoteStream);
img.Save(URL, System.Drawing.Imaging.ImageFormat.Jpeg);
response.Close();
RemoteStream.Close();
SR.Close();


Thanks
Posted
Updated 28-Nov-10 0:05am
v2
Comments
69Icaro 28-Nov-10 5:54am    
I haven't understood what you want to do... can you explain a bit more?
Is your code in an ASP.NET page, or what?
JF2015 28-Nov-10 6:05am    
Edited to improve spelling and code formatting.

1 solution

I'm not sure what is wrong with your code, but this works for me, perhaps you can refactor it:
C++
WebRequest request = WebRequest.Create("http://www.dur.ac.uk/images/about/new/DurhamCastleCathedral.jpg");
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = request.GetResponse();
Image image = Image.FromStream(response.GetResponseStream());
image.Save(@"c:\foo.jpg", ImageFormat.Jpeg);
response.Close();


You should be able to read from the response stream into the output file stream directly, if you don't need the intermediate Image object. This will save memory, and might be quicker under certain circumstances.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900