Image to byte array from a url

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.


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);
}

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);
     }
}

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

Example:

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