tags:

views:

369

answers:

3

Hi

I'm trying to take a screen shot of an applet running inside a browser. The applet is using JOGL (OpenGL for Java) to display 3D models. (1) The screen shots always come out either black or white.The current solution uses the usual GDI calls. Screen shots of applets not running OpenGL are fine.
A few examples of JOGL apps can be found here https://jogl-demos.dev.java.net/ (2) Another thing I'm trying to achieve is to get the scrollable area inside the screen shot as well.

I found this code on the internet which works fine except for the 2 issues mentioned above.

import win32gui as wg  
import win32ui as wu  
import win32con  

def copyBitMap(hWnd, fname):  
    wg.SetForegroundWindow(hWnd)  
    cWnd = wu.CreateWindowFromHandle(hWnd)  
    rect = cWnd.GetClientRect()  
    (x,y) = (rect[2] - rect[0], rect[3] - rect[1])  
    hsrccDc = wg.GetDC(hWnd)  
    hdestcDc = wg.CreateCompatibleDC(hsrccDc)  
    hdestcBm = wg.CreateCompatibleBitmap(hsrccDc, x, y)  
    wg.SelectObject(hdestcDc, hdestcBm.handle)  
    wg.BitBlt(hdestcDc, 0, 0, x, y, hsrccDc, rect[0], rect[1], win32con.SRCCOPY)  
    destcDc = wu.CreateDCFromHandle(hdestcDc)  
    bmp = wu.CreateBitmapFromHandle(hdestcBm.handle)  
    bmp.SaveBitmapFile(destcDc, fname)  
A: 

Unless you are trying to automate it, I would just use a Firefox extension for this. There are a number of them returned from a search for "screenshot" that can take a screenshot of the entire browser page including the scrollable area:

However, I apologize, I don't know enough about Python to debug your specific issue if you are indeed trying to do it programmatically.

Ricket
Thanks Ricket but I am trying to automate it. I have used Screengrab but even that comes up with a black area where the applet is in the webpage. Plus I need the method to be browser independent.
binarybug
A: 

Here is one way to do it by disabling dwm (Desktop Window Manager) composition before taking the screen shot, but this causes the whole screen to blink whenever its enabled/disabled.

from ctypes import WinDLL
from time import sleep  
import win32gui as wg  
import win32ui as wu  
import win32con  

def copyBitMap(hWnd, fname):  
    dwm = WinDLL("dwmapi.dll")
    dwm.DwmEnableComposition(0)

    wg.SetForegroundWindow(hWnd)
    # Give the window sometime to redraw itself
    sleep(2)
    cWnd = wu.CreateWindowFromHandle(hWnd)
    rect = cWnd.GetClientRect()
    (x,y) = (rect[2] - rect[0], rect[3] - rect[1])
    hsrccDc = wg.GetDC(hWnd)
    hdestcDc = wg.CreateCompatibleDC(hsrccDc)
    hdestcBm = wg.CreateCompatibleBitmap(hsrccDc, x, y)
    wg.SelectObject(hdestcDc, hdestcBm.handle)
    wg.BitBlt(hdestcDc, 0, 0, x, y, hsrccDc, rect[0], rect[1], win32con.SRCCOPY)
    destcDc = wu.CreateDCFromHandle(hdestcDc)
    bmp = wu.CreateBitmapFromHandle(hdestcBm.handle)
    bmp.SaveBitmapFile(destcDc, fname)

    dwm.DwmEnableComposition(1)
binarybug
A: 

Grabbing an OpenGL window may be quite hard in some cases, since the OpenGL is being rendered by the GPU directly into its frame buffer. The same applies to DirectX windows and Video overlay windows.

rep_movsd