Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I used the following code to get Image of the given html.. The asp controls that I used here are TextBox, Image, Button. The event for the button click after providing the website link is as follows..

C#
protected void GetImage_Click(object sender, EventArgs e)
{
string mySite = TextBox1.Text;
WebClient wc = new WebClient();
string getData = wc.DownloadString(mySite);
string result = Regex.Match(getData, "<img.+?src=[\"'](.+?)[\"'].+?>",RegexOptions.IgnoreCase).Groups[1].Value;
Image1.ImageUrl= result;
}


I dont know what's the problem in my code. I am always getting into trouble while I use REGULAR EXPRESSION... Please do suggest me a solution that works fine...
Posted

1 solution

I just tried the regex itself with a sample html file, and it returned the first image URL in the file.

So, you need to look at the way you are downloading the data, and what it actually downloads - it may not be what you think!

BTW: It is a good idea to read the Match into a Match class instance, and check the Success property before trying to access the results. And use named groups rather than anonymous, so you don't have to use magic numbers in your code!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900