Click here to Skip to main content
15,174,547 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i make get request server response me jpeg file.

C#
HttpWebRequest picrequest = (HttpWebRequest)HttpWebRequest.Create(captchaUrl);
HttpWebResponse picResponse = (HttpWebResponse)picrequest.GetResponse();
StreamReader srr = new StreamReader(picResponse.GetResponseStream(), Encoding.UTF8);
string htmlz = srr.ReadToEnd();

in htmlz i have "����JFIF``��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v80....".

How save it to MemoryStream of download as jpeg file. I dont use Webclient.
Posted

Try this way.
C#
WebRequest picrequest = WebRequest.Create( captchaUrl );
using( WebResponse response = picrequest.GetResponse() ) {
    using( Stream responseStream = response.GetResponseStream() ) {
        Image myImage = Image.FromStream(responseStream);


    }
}

Edit

And then you can save myImage using Image.Save Method in the image format you want.

http://msdn.microsoft.com/en-us/library/ytz20d80(v=vs.110).aspx
   
v2
Comments
Sergey Alexandrovich Kryukov 20-Jun-14 23:13pm
   
Apparently... 5ed.
But I added important advice to it in Solution 2, with reference to my code, please see.
—SA
[no name] 24-Jun-14 15:14pm
   
Thank you SA.
In addition to Solution 1: strictly speaking, to save something to a file, you don't need to know that it is the image or something else. You can just transparently save the response stream we it is. For an example, please see my HTTP downloader (which, by they way, can continue downloading partially downloaded file after it was interrupted): how to download a file from internet[^].

See also:
FTP: Download Files[^],
how to download a file from the server in Asp.net 2.0[^].

—SA
   

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