Snap a web page as image

Published on by Yousfi Benameur

 
Capture contents of visible or invisible windows & web pages
with PRINTWINDOW API

 

This code captures the contents of visible, invisible windows and web pages as images jpg,png,gif,bmp,tiff...

The normal way of capturing the contents of a window into a bitmap is creating a memory device context (CreateCompatibleDC), creating a device-dependent bitmap (CreateCompatibleBitmap) or a DIB section (CreateDIBSection), selecting the bitmap into the memory DC (SelectObject), and then bitblt from the window's device context (GetWindowDC) into the memory DC (Bitblt).
After that, a copy of the contents of the window as it appears on the screen is stored in the bitmap.


the problem is that bitblt API works only for visible windows, all gdiplus codes in general work only for visible windows. if the window is hidden, or partially blocked with other windows the non-visible part of the window will be clipped in the device context returned from GetWindowDC. In other words, that part of the window can't be captured using a simple BitBlt.

To capture any window, completely visible, partially visible, or complete invisible, Win32 API
provides two special messages, WM_PRINT and WM_PRINTCLIENT. Both these messages take a device context handle as a parameter, and the window handling these messages is supposed to draw itself or its client area into the device context provided.

i tried this trick but without success.A black image raises and dont render the real copy of the web page.Maybe there is some works on bindevent WM_PAINT and WM_PRINT...Only a black frame is returned here.

Tried also the IE interface iHTMLelementRender.drawToDC (loading the MSHTML.TLB on VFP browser shows that).)

Finally the PrintWindow of the DIBAPI32.dll (fre download on the web) of the visible screen ,making it visible, and the result is positive.Icaptured the entier window contents as an image bmp,jpg,gif,png,Tiff,Emf,... with gdiplus can crop, resize,the desired image returned as well

Put a valid url(rightclick to paste url from clipboard), choce the image type,( even resizing dimensions),the max delay to wait  (sec) and Go!

Before begining:

Make sure if dibiApi32.dll is in your system32 otherwise simply google and download it.(put it in windows\system32).

the internet must be connected otherwise the code tells you for this.
the code recquires system.app to work.i Made a copy of system.app in download.

 

Tested on Windows XP SP3 ,win8.1 and VFP9 SP2-Recquiert system.app of gdiplusX in folder.

Author : Yousfi Benameur El Bayadh ALGERIA
Wednesday 04 april 2013

In code *2* i added a simple solution with a virtual printer to get any url as pdf or xps file with a virtual printer as shipped with windows10.

 

Can also use pdfCreator to write directly the web (url)  page on pdf,jpg...Please test & report any bug.
Can also use pdfCreator to write directly the web (url)  page on pdf,jpg...Please test & report any bug.
Can also use pdfCreator to write directly the web (url)  page on pdf,jpg...Please test & report any bug.

Can also use pdfCreator to write directly the web (url) page on pdf,jpg...Please test & report any bug.

Click on code to select [then copy] -click outside to deselect


*2*added on tuesday  22 march 2016; 11:04:26
*this command line prints directly  any web page (or local web page) without show it (invisible).
*Rundll32.exe %windir%\system32\mshtml.dll,PrintHTML "http://yousfi.over-blog.com/2016/01/a-configurable-desktop-calendar.html"
*this is under security considerations(i see sometimes the web site source dont admit this printing (robots)).
*i have virtual printer as PDFCreator can print to file as pdf,jpg,png,txt.......
*in window10 can set as default printer "Microsoft Print to PDF" or "Microsoft XPS Document Writer" and run this code.outpit is pdf or xps file.

local m.url,m.xx
m.url="http://yousfi.over-blog.com/2016/01/unicode-texts-and-vfp-continuation.html"
m.xx="Rundll32.exe "+ADDBS(GETENV('windir'))+'system32\mshtml.dll,PrintHTML "'+m.url+'"'

local oshell
oshell=newObject("wscript.shell")
oshell.run(m.xx)
oshell=null
retu


To be informed of the latest articles, subscribe:
Comment on this post