Save image to disk from HTTP URL

G

George

Your code is lame.
After you got the stream you can just save it to file.
You do not need to create
System.Drawing.Image img = System.Drawing.Image.FromStream(remoteStream);
and then save it to file.

George.
 
M

mxdev, MCDBA, MCPD, MCITP, MCP

to say that my code is just bad - please provide your code to save data from
Stream to file on disk..

the reason to create image object for me is to make some manupulations with
image before saving to disk (resize, crop, etc), so this code may be useful
in some situations.

Stream object is the stream returned by WebResponse.GetResponseStream();
this Stream doesn't have method to save its content directly to file .
so you need to
1. read data using StreamReader object, or other reader
2. save this data into some variable
3. save this data into file.

you have to store the data from stream into variable (memory),
so my approach to store this data in Image object will not consume much more
memory.

to create StreamReader you have to deal with Encoding of the stream..

StreamReader stream = new StreamReader(webresponse.GetResponseStream());
String t = stream .ReadToEnd();

what to do next with with data?

or you advice to get data from the stream as array of bytes and then save to
disk ?


so i don't think any such code will be easier.

will be good to see your several lines of working code to save data from
stream to file .

i know the simplest way is to use this code:

WebClient client = new WebClient();
client.DownloadFile(url, filename);
 
G

George

I am not talking about easier code.
Do you know how many things are happen in one small line
System.Drawing.Image img = System.Drawing.Image.FromStream(remoteStream);

If image is JPEG then stream is decompressed with fairly expensive
arithmetic operations.

Then you do opposite operation with this line
img.Save( filename, System.Drawing.Imaging.ImageFormat.Jpeg );

Plus you are loosing on image quality. This is what JPEG format is about. It
compresses image by loosing quality.
Open up any tool like Photoshop and it will allow you to choose the
compression level.

Plus if you download image with the size 2048x1024 do you know how many
bytes it's going to eat when you decompress it.
2048*1024*4 = 8 Megabytes.

So conclusion: it's just plain bad code.
-------------------------
Attempt to do it with StreamReader and to do this
String t = stream .ReadToEnd();
probably even worse (not sure which one worse actually).

Never convert to string binary data.
---------------------------

The best solution to save stream to file would be something like this

Stream st = WebResponse.GetResponseStream();
using(Stream fs = File.Open(path, FileMode.Create) )
{
byte []buf = new byte[1000];
int iRead = 0;
do
{
iRead = st.Read(buf, 0, buf.lengh);
if( iRead > 0 )
fs.Write(buf, 0, iRead);
} while(iRead > 0 )
}

Is it more than those 2 lines you wrote: no
Is it much better code : Yes.

PS: you can throw out all your MCPMBDBA titles.

George.



"mxdev, MCDBA, MCPD, MCITP, MCP" <mxdev, MCDBA, MCPD, MCITP,
(e-mail address removed)> wrote in message
 
S

S. Justin Gengo

Yes, if you want to manipulate the image before the save you have to get it
into some type of image object. If all you need to do is get the image to
the server then a file stream will be less resource intensive.

As far as uploading and manipulating images I have a free - including source
code - Image Upload server control available here:
http://www.aboutfortunate.com/Component-Library.aspx

Instructions and a demo are here:
http://www.aboutfortunate.com/Component-Library/Image-Upload-Object.aspx

With the Image Upload component you can optimize the uploaded image
converting to .gif, .png, or .jpg, resize, and/or convert the image to black
and white.

It doesn't allow cropping yet, but I hope to add that shortly.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche




"mxdev, MCDBA, MCPD, MCITP, MCP" <mxdev, MCDBA, MCPD, MCITP,
(e-mail address removed)> wrote in message
 

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