473,322 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 15557
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.