472,370 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,370 software developers and data experts.

win32ui vs wxPy screen capture multi monitor

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.

Expand|Select|Wrap|Line Numbers
  1. import win32gui,  win32ui,  win32con, win32api
  2.  
  3. hwnd = win32gui.GetDesktopWindow()
  4. print hwnd
  5.  
  6. # you can use this to capture only a specific window
  7. #l, t, r, b = win32gui.GetWindowRect(hwnd)
  8. #w = r - l
  9. #h = b - t
  10.  
  11. # get complete virtual screen including all monitors
  12. SM_XVIRTUALSCREEN = 76
  13. SM_YVIRTUALSCREEN = 77
  14. SM_CXVIRTUALSCREEN = 78
  15. SM_CYVIRTUALSCREEN = 79
  16. w = vscreenwidth = win32api.GetSystemMetrics(SM_CXVIRTUALSCREEN)
  17. h = vscreenheigth = win32api.GetSystemMetrics(SM_CYVIRTUALSCREEN)
  18. l = vscreenx = win32api.GetSystemMetrics(SM_XVIRTUALSCREEN)
  19. t = vscreeny = win32api.GetSystemMetrics(SM_YVIRTUALSCREEN)
  20. r = l + w
  21. b = t + h
  22.  
  23. print l, t, r, b, ' -> ', w, h
  24.  
  25. hwndDC = win32gui.GetWindowDC(hwnd)
  26. mfcDC  = win32ui.CreateDCFromHandle(hwndDC)
  27. saveDC = mfcDC.CreateCompatibleDC()
  28.  
  29. saveBitMap = win32ui.CreateBitmap()
  30. saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
  31. saveDC.SelectObject(saveBitMap)
  32. saveDC.BitBlt((0, 0), (w, h),  mfcDC,  (l, t),  win32con.SRCCOPY)
  33. saveBitMap.SaveBitmapFile(saveDC,  'screencapture.bmp')
  34.  
Dec 14 '06 #1
5 14949
and to save that file as jpg, png... you can use PIL:

Expand|Select|Wrap|Line Numbers
  1. bmpinfo = saveBitMap.GetInfo()
  2. print bmpinfo
  3. bmpstr = saveBitMap.GetBitmapBits(True)
  4.  
  5. import Image
  6. im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1)
  7. #im.save('screencapture.jpg', format = 'jpeg', quality = 85)
  8. im.save('screencapture.png', format = 'png')
  9.  
Dec 14 '06 #2
to do the whole thing even easier use wx:

Expand|Select|Wrap|Line Numbers
  1. import wx
  2.  
  3. app = wx.PySimpleApp()
  4.  
  5. context = wx.ScreenDC()
  6. r, b = context.GetSize()
  7.  
  8. # i have a second monitor left of my primary, so these value are negativ
  9. l, t = (-1280, -256) # coulfn't find a wx function to get these
  10.  
  11. w, h = (r - l, b - t)
  12. bitmap = wx.EmptyBitmap(w, h, -1)
  13.  
  14. memory = wx.MemoryDC()
  15. memory.SelectObject(bitmap)
  16. memory.Blit(0, 0, w, h, context, l, t)
  17. memory.SelectObject(wx.NullBitmap)
  18.  
  19. #bitmap.SaveFile("screencapture.bmp", wx.BITMAP_TYPE_BMP)
  20. #bitmap.SaveFile("screencapture.jpg", wx.BITMAP_TYPE_JPEG)
  21. bitmap.SaveFile("screencapture.png", wx.BITMAP_TYPE_PNG)
  22.  
Dec 14 '06 #3
bartonc
6,596 Expert 4TB
Wow. Thanks nobugus. This is very useful.
Dec 14 '06 #4
bartonc
6,596 Expert 4TB
Wow. Thanks nobugus. This is very useful.
This one is really worth a bump to the top.
Dec 17 '06 #5
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:

Expand|Select|Wrap|Line Numbers
  1. saveDC.DeleteDC()
  2. 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!
Sep 20 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
8
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:...
1
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 =...
8
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?...
1
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...
2
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...
11
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...
4
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...
0
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.