Get any Link - Thumbnail (like facebook does)

Hello,
I'd like to find a code that returns a link thumbnail (image)
just like facebook does..
Preferably .Net code.
… I suppose that the algorithm should:
First look at the html "og:image" meta tag.
and if it doesn't exist - return the first available image in the html. like: xpath = "//img"
right?
Thanks, Aryeh.
tuchfeldAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
Phantom JS
Use PhantomJS to create screenshots. PhantomJS comes with an example called rasterize.js, which does exactly this. Example:
phantomjs rasterize.js http://www.microsoft.com/ ms.png

Open in new window


Selenium Client Driver for C#
Taking screenshots with Selenium WebDriver (http://docs.seleniumhq.org/download/). Sampel code:
The code to take a screenshot is not complex:
using OpenQA.Selenium.IE;
using OpenQA.Selenium;
using System.Drawing.Imaging;
 
public void TakeScreenshot(IWebDriver driver, string saveLocation)
{
    ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;
    Screenshot screenshot = screenshotDriver.GetScreenshot();
    screenshot.SaveAsFile(saveLocation, ImageFormat.Png);
}

IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.microsoft.com");
TakeScreenshot(driver, @"C:\temp\ms.png");

Open in new window


Use a web service
Here is a list of some services: http://webresourcesdepot.com/10-free-website-thumbnail-generation-services/

Create your own service
You can use a headless browser to accomplish this. WebKit is a good candidate (the underlying engine for PhantomJS, Chrome)
0
tuchfeldAuthor Commented:
Thanks,
but as understand: these are page snapshots (which in fact I already know how to do, using System.Windows.Forms.WebBrowser.DrawToBitmap).
Do they support "og:image" meta tag
as FIRST SUGGESTION for the page Thumbnail?
0
MlandaTCommented:
Ooops... I downplayed the Open Graph Image issue. You can pass the URL to the OpenGraph-Net libray (GitHub or Nuget) which will parse your HTML and extract the Open Graph metadata and put it into a nice object for you.
OpenGraph graph = OpenGraph.ParseUrl("http://www.microsoft.com/");
if(graph.Image!=null) {
     Uri imgUrl = graph.Image;
     //download the image and do as you please
}

Open in new window



Sidenote: System.Windows.Forms.WebBrowser.DrawToBitmap is based on the IE based web browser control. IE... well... even Microsoft have thrown in the towel :) It's slow and because it's not standards compliant, there are lots of cases where sites appear distorted in IE. But anyway, if it works well for you, why change it! Lool
1
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

tuchfeldAuthor Commented:
OK, Thanks for the valuable info..
Now back to the case when we do NOT have an image meta tag.
Are the solutions you suggested above return the FIRST AVAILABLE IMAGE (as facebook does)
or a snapshot of the page?
take as an example this link:
http://www.websitemanager.co.il/index_ExEx.htm
Which should return:
http://www.websitemanager.co.il/images/WSM_Title.gif
(and not a snapshot of the page..).
0
MlandaTCommented:
Without the og:image metadata, I don't really see  how you will know? Does one just parse the HTML and take the first image? The biggest image by dimensions? I'm not sure on this one
0
tuchfeldAuthor Commented:
I can let the user decide which image to choose...
and get the images by scanning: WebBrowser.Document.All
(should do it AFTER webBrowser.ReadyState = WebBrowserReadyState.Complete).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tuchfeldAuthor Commented:
Eventually, I didn't find an automatic answer but I let the user to choose.
My Thanks to MlandaT for assisting me to get to this conclusion.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.