'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.



Solution 1:[1]

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

Solution 2:[2]

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

Example:

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

Solution 3:[3]

Try following method:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dunc
Solution 2 Christo Carstens
Solution 3 AliNajafZadeh