// This code snippet can be used to retrieve any image from any HTML content or source
//If youre' having any problem in coding, you can
download the sample project I have written in C# (.net 2008-SP1)
//I hope you'll like this code snippet and I know It can be written much more flexible without writing all those extensions. But to save time and energy I did it in such a way that I can achieve my goal //easily...hehe
using System.Text;
private void button1_Click(object sender, EventArgs e)
{
label2.Text = getHTMLImage("WRITE YOUR HTML SOURCE HERE WHICH CONTAINS A <IMG> TAG");
}
protected virtual String getHTMLImage(string Body) {
String bodyX = Body.ToLower();
int getPosition = -1;
int getPositionY = -1;
String Extension = "";
getPositionY = bodyX.IndexOf("src=");
//-----------------------------------------------------
//1st phaze
String ImagePath = bodyX.Remove(0, getPositionY + 5);
//2nd phaze
if (getPosition == -1)
{
getPosition = ImagePath.IndexOf(".jpg" + '"');
Extension = ".jpg";
}
if (getPosition == -1)
{
getPosition = ImagePath.IndexOf(".png" + '"');
Extension = ".png";
}
if (getPosition == -1)
{
getPosition = ImagePath.IndexOf(".jpeg" + '"');
Extension = ".jpeg";
}
if (getPosition == -1)
{
getPosition = ImagePath.IndexOf(".gif" + '"');
Extension = ".gif";
}
if (getPosition == -1)
{
getPosition = ImagePath.IndexOf(".bmp" + '"');
Extension = ".bmp";
}
try
{
ImagePath = ImagePath.Remove(getPosition, ImagePath.Length - getPosition);
}
catch {
ImagePath = "no Image in the HTML Code";
Extension = "";
}
return ImagePath + Extension;
}
Regular Expressions are great particularly if use Expresso a free download from
http://www.ultrapico.com/ExpressoDownload.htm however as the complexity builds up, they become more and more unweildy.
May I respectfully suggest that you publish your snippets project on CodePlex, and include an LGPL like license that clearly establishes that the code may be copied without fee.
Click "Mark as Answer" on the post that helped you.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
None
0 Points
1 Post
Code Snippet: Retrieve an Image from HTML Source
Dec 07, 2008 10:29 PM|yaasir|LINK
// This code snippet can be used to retrieve any image from any HTML content or source
//If youre' having any problem in coding, you can download the sample project I have written in C# (.net 2008-SP1)
//I hope you'll like this code snippet and I know It can be written much more flexible without writing all those extensions. But to save time and energy I did it in such a way that I can achieve my goal //easily...hehe
Contributor
3212 Points
703 Posts
Re: Code Snippet: Retrieve an Image from HTML Source
Dec 08, 2008 02:10 AM|ASP.NET Dev|LINK
Hi,
Good code.
I think it will be more simple and faster if we use regular expression to find img tags.
Check this http://www.dreamincode.net/forums/showtopic47500.htm
All-Star
44551 Points
13496 Posts
MVP
Re: Code Snippet: Retrieve an Image from HTML Source
Dec 11, 2008 04:45 AM|TATWORTH|LINK
The HTML Agility Pack at http://www.codeplex.com/htmlagilitypack will parse HTML
Regular Expressions are great particularly if use Expresso a free download from http://www.ultrapico.com/ExpressoDownload.htm however as the complexity builds up, they become more and more unweildy.
May I respectfully suggest that you publish your snippets project on CodePlex, and include an LGPL like license that clearly establishes that the code may be copied without fee.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239