Just posting a solution to the problem of capturing the whole virtual screen area, including all monitors with python and pywin32.
Because this doesn't work even with PIL's imagegrab module. -
import win32gui, win32ui, win32con, win32api
-
-
hwnd = win32gui.GetDesktopWindow()
-
print hwnd
-
-
# you can use this to capture only a specific window
-
#l, t, r, b = win32gui.GetWindowRect(hwnd)
-
#w = r - l
-
#h = b - t
-
-
# get complete virtual screen including all monitors
-
SM_XVIRTUALSCREEN = 76
-
SM_YVIRTUALSCREEN = 77
-
SM_CXVIRTUALSCREEN = 78
-
SM_CYVIRTUALSCREEN = 79
-
w = vscreenwidth = win32api.GetSystemMetrics(SM_CXVIRTUALSCREEN)
-
h = vscreenheigth = win32api.GetSystemMetrics(SM_CYVIRTUALSCREEN)
-
l = vscreenx = win32api.GetSystemMetrics(SM_XVIRTUALSCREEN)
-
t = vscreeny = win32api.GetSystemMetrics(SM_YVIRTUALSCREEN)
-
r = l + w
-
b = t + h
-
-
print l, t, r, b, ' -> ', w, h
-
-
hwndDC = win32gui.GetWindowDC(hwnd)
-
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
-
saveDC = mfcDC.CreateCompatibleDC()
-
-
saveBitMap = win32ui.CreateBitmap()
-
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
-
saveDC.SelectObject(saveBitMap)
-
saveDC.BitBlt((0, 0), (w, h), mfcDC, (l, t), win32con.SRCCOPY)
-
saveBitMap.SaveBitmapFile(saveDC, 'screencapture.bmp')
-
5 15596
and to save that file as jpg, png... you can use PIL: - bmpinfo = saveBitMap.GetInfo()
-
print bmpinfo
-
bmpstr = saveBitMap.GetBitmapBits(True)
-
-
import Image
-
im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1)
-
#im.save('screencapture.jpg', format = 'jpeg', quality = 85)
-
im.save('screencapture.png', format = 'png')
-
to do the whole thing even easier use wx: - import wx
-
-
app = wx.PySimpleApp()
-
-
context = wx.ScreenDC()
-
r, b = context.GetSize()
-
-
# i have a second monitor left of my primary, so these value are negativ
-
l, t = (-1280, -256) # coulfn't find a wx function to get these
-
-
w, h = (r - l, b - t)
-
bitmap = wx.EmptyBitmap(w, h, -1)
-
-
memory = wx.MemoryDC()
-
memory.SelectObject(bitmap)
-
memory.Blit(0, 0, w, h, context, l, t)
-
memory.SelectObject(wx.NullBitmap)
-
-
#bitmap.SaveFile("screencapture.bmp", wx.BITMAP_TYPE_BMP)
-
#bitmap.SaveFile("screencapture.jpg", wx.BITMAP_TYPE_JPEG)
-
bitmap.SaveFile("screencapture.png", wx.BITMAP_TYPE_PNG)
-
Wow. Thanks nobugus. This is very useful.
Wow. Thanks nobugus. This is very useful.
This one is really worth a bump to the top.
First, many thanks to nobugus for this post, I had been hunting around for a way to do this for a while now, and this post was a huge boon!
It's worth noting though, that the code as is in the first post has a memory leak, and that if you wrap his code in a function take enough screenshots it will eventually crash with a windows error code 8. (On my system, this happens somewhere between 100 and 200 screenshots).
Adding the following after saving the screenshot (or doing whatever you like with it) seems to close the leak: - saveDC.DeleteDC()
-
win32gui.DeleteObject(saveBitMap.GetHandle())
I don't understand the details of why python/pywin32 garbage collection isn't handling this when the variables leave scope, but I thought I'd post this in case anyone else had the same problem. I experimented with leaving out just one line or the other, but both seem necessary. If anyone wants to explain to me why this works, I'm all ears!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Me Mine |
last post by:
i,
I have trying to code a small console app that will
allow a user to select a window and then create a
screen capture of the window. I haven't been able to
figure out how to do the screen...
|
by: Els |
last post by:
Hi, i have the following script in the head of my document:
<script language="Javascript">
<!--
// ***********************************************
// AUTHOR: WWW.CGISCRIPT.NET, LLC
// URL:...
|
by: Chris Lambacher |
last post by:
Hi,
This question has come up a few times on the list with no one giving a public
answer. How do you use CreatePrintDialog from win32ui?
About a year ago someone posted that:
dlg =...
|
by: gregory_may |
last post by:
Is there a way to grab a "Screen Shot" that includes "Tool Tips"? I saw
this code someplace, cant remember where. But it doesnt grab "Tool Tips".
Is there a better way to do this in .net?...
|
by: Patrick |
last post by:
need some code for getting screenshots from the secondary monitor on dual
monitor display.
Here's a bit of the code for a screen from the primary monitor.
Bitmap MyImage = new...
|
by: Schorschi |
last post by:
If I run my application from the explorer on my 2nd monitor, the
screen object reported via Screen.FromControl(theForm).Bounds is the
primary screen bounds not the screen that my application is...
|
by: Arcer P |
last post by:
Situation 1 (erroneous):
TaskBar changes
Screen.PrimaryScreen.WorkingArea still returns the old rectangle
Situation 2 (OK)
TaskBar changes
Form Quits and Reloads...
|
by: Zorpiedoman |
last post by:
In a multi-monitor environment, a form I am creating at run time keeps
placing itself on the primary screen, even if the main form of the program
making the call is on the secondary screen.
How...
|
by: Franklin M. Gauer III |
last post by:
Hi,
We have an application running that uses FULL SCREEN ANCHORING in Windows
Forms. The application runs fine on all of our desktops and some laptops.
We are having problems with certain Dell...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |