Load image from url save it as byte

bytearray c# html-agility-pack image

Question

I getting one image with HTMLAgilityPack and then I want to load it as byte so I could save it in database.

byte[] bIMG = File.ReadAllBytes(doc.DocumentNode.SelectSingleNode("//img[@class='image']").Attributes["src"].Value);

But it says URI formats are not supported. how else I can do that?

EDIT: doc.DocumentNode.SelectSingleNode("//img[@class='image']").Attributes["src"].Value gives a link

Accepted Answer

The System.IO.File class can't read web URIs - you can use the WebClient for this:

byte[] imageAsByteArray;
using(var webClient = new WebClient())
{
    imageAsByteArray = webClient.DownloadData("uri src");
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why