Thursday, April 5, 2012

convert image to base64


For this u need to download Html agility


protected void Page_Load(object sender, EventArgs e)
{
HtmlWeb htmlWeb = new HtmlWeb();
HtmlAgilityPack.HtmlDocument document = htmlWeb.Load("http://www.fortunehotels.in/");
foreach (HtmlNode someNode in document.DocumentNode.SelectNodes("//img[@src]"))
{
if (someNode.Attributes.Contains("src"))
{
if (someNode.Attributes["src"].Value.EndsWith(".jpg"))
{
if (someNode.Attributes["src"].Value != "admin/UploadImage/summer-packages2010717138.jpg")
{
if (someNode.Attributes["src"].Value != "admin/UploadImage/101X108201243356.jpg")
{
WebClient wc = new WebClient();
byte[] data = wc.DownloadData(someNode.Attributes["src"].Value);
string base64ImageString = ConvertBytesToBase64(data);
someNode.Attributes["src"].Value = "data:image/jpg;base64," + base64ImageString;
myImage.Src = "data:image/jpg;base64,"+base64ImageString;
}
document.Save("C:/Users/sandeep_2/Desktop/fortune.htm");
}
}
}
}
}
public string ConvertBytesToBase64(byte[] imageBytes)
{
return Convert.ToBase64String(imageBytes);
}

No comments:

Post a Comment