75

I have a hyperlink which has a image.

I need to read/load the image from that hyperlink and assign it to a byte array (byte[]) in C#.

Thanks.

Ben
  • 51,262
  • 47
  • 169
  • 217
Sharpeye500
  • 8,345
  • 25
  • 85
  • 139

4 Answers4

167

WebClient.DownloadData is the easiest way.

var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData("http://www.google.com/images/logos/ps_logo2.png");

Third party edit: Please note that WebClient is disposable, so you should use using:

string someUrl = "http://www.google.com/images/logos/ps_logo2.png"; 
using (var webClient = new WebClient()) { 
    byte[] imageBytes = webClient.DownloadData(someUrl);
}
Ben
  • 51,262
  • 47
  • 169
  • 217
Josh
  • 66,410
  • 14
  • 139
  • 154
  • 1
    Thanks, am getting closer, not getting the image bytes, it will be much help if you throw some quick example. – Sharpeye500 Jan 05 '11 at 00:12
  • 1
    What are you getting then? You may want to grab a tool called Fiddler that will show you the request and response that might help you troubleshoot problems. It really doesn't get any simpler than WebClient.DownloadData. – Josh Jan 05 '11 at 00:13
  • 1
    Thanks , but i get "The remote server returned an error: (404) Not Found." – Sharpeye500 Jan 05 '11 at 00:18
  • 1
    Then check out Fiddler and see if you can spot a difference between doing it in the browser and doing it from code. A 404 usually indicates you have the wrong URL. – Josh Jan 05 '11 at 00:20
  • 23
    Please note that WebClient is disposable, so you should a `using`, like so: `string someUrl = "http://www.google.com/images/logos/ps_logo2.png";` `using (var webClient = new WebClient()) {` ` byte[] imageBytes = webClient.DownloadData(someUrl);` ` // do something with imageBytes` `}` (Sorry for the messed up layout.) – Bondt Jan 16 '13 at 15:31
  • 1
    @Bondt Same code written as per you have, but getting still the same err `The remote server returned an error: (404) Not Found` – PPB Jan 13 '16 at 14:26
  • 1
    @Sharpeye500 I am getting same err, have you got any solution on that? – PPB Jan 13 '16 at 14:27
  • 1
    @Josh i am using this URL - `http://img.youtube.com/vi/3p0PCs2jDvM/hqdefault.jpg` – PPB Jan 13 '16 at 14:37
  • 1
    Works like charm.. Thanks – Arjun Jun 22 '19 at 05:48
  • WebClient is now obsolete. See the HttpClient version in @Dunc's answer for modern alternative. – Peter David Carter Apr 22 '22 at 09:40
4

If you need an asynchronous version:

using (var client = new HttpClient())
{
    using (var response = await client.GetAsync(url))
    {
        byte[] imageBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
     }
}
Dunc
  • 17,414
  • 6
  • 80
  • 98
3

.NET 4.5 introduced WebClient.DownloadDataTaskAsync() for async usage.

Example:

using ( WebClient client = new WebClient() )
{
  byte[] bytes = await client.DownloadDataTaskAsync( "https://someimage.jpg" );
}

0

Try following method:

public byte[] UdfGetByteFromImageURL(String StrImageUrl)
{
    using (var webClient = new WebClient()) 
    { 
            byte[] imageBytes = webClient.DownloadData(StrImageUrl);
        return imageBytes;
    }
}
AliNajafZadeh
  • 414
  • 3
  • 18