3

Is there any way to capture an image of a web page using a C++ CGI? I've been searching high and low, and while I've found a number of implementations in everything from Perl to C#, haven't been able to find any implementations in C++.

The idea is for a user visiting a site to be able to specify a URL. The script would then take a picture of the URL's website, and then show load it to the C++ CGI website I am building.

Any help on this would be much appreciated!

Thanks!

Example in Perl: Webthumb

7
  • 1
    What code did the C# implementations use? Almost anything you can write in C# can be easily ported to a Win32 C++ app. May 7, 2011 at 9:57
  • As in, a method where the argument is a URL?
    – Bart
    May 7, 2011 at 9:58
  • @Bart - aye, as in a method that would take in a URL as an argument, and take a picture of the said URL, and save it to a folder (or show it directly on another website). Basically, I am developing a CGI website in C++, and as part of the requirement I need to be able to show webthumbs of websites. Though since nobody in their right mind would write a CGI website in C++, there source material is a bit short :)
    – ElfsЯUs
    May 7, 2011 at 10:07
  • @Cody - will try to find those again... though I think they required special C# libraries...
    – ElfsЯUs
    May 7, 2011 at 10:08
  • Also, thumbnail is presumably the favicon? That's a simple HTTP GET. If it's more, there are about a zillion webservices that will allow you to get a thumbnail using a cgi, or you can just host such a service youself and call it. That way, it perhaps need not be in c++?
    – sehe
    May 7, 2011 at 10:08

1 Answer 1

5

You need to render the webpage in order to create a snapshot. To render the page in C++ you need to include a browser engine. You can do this easily using Qt (a toolkit for c++). This is taken from the documentation of Qt and is all you need to show a webpage.

QWebView *view = new QWebView(ui->centralWidget);
view->load(QUrl("http://qt.nokia.com/"));
view->show();

The view object has a loadFinished signal. You could connect some code to this signal. When the page is rendered you tak a snapshot as described here. It boils down to:

QPixmap::grabWindow(mainwindow->winId())

When you have got the screenshot you can return the bytes on stdout from your cgi and your done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.