Get image with HttpWebRequest and save it locally

Posted on 2009-04-29
Last Modified: 2013-12-17
Hi you experts.

I would like to get an image with a HttpWebRequest let's say from the url: and save it to a local directory as ex. c:\test2.jpg
Any code snippets of how this can be achieved will be highly appreciated.

Thank you all in advance.
Question by:adktd
    1 Comment
    LVL 12

    Accepted Solution

    Try Below
    static public byte[] GetBytesFromUrl(string url)
    byte[] b;
    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
    WebResponse myResp = myReq.GetResponse();
    Stream stream = myResp.GetResponseStream();
    //int i;
    using (BinaryReader br = new BinaryReader(stream))
    //i = (int)(stream.Length);
    b = br.ReadBytes(500000);
    return b;
    And use the following code to save it to disk.
    static public void WriteBytesToFile(string fileName, byte[] content)
    FileStream fs = new FileStream(fileName, FileMode.Create);
    BinaryWriter w = new BinaryWriter(fs);

    Open in new window


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now