Skip to content

ImageGrab cannot get correct resolution screenshot for Windows 10 zoom #3626

@HanqingTony

Description

@HanqingTony

What did you do?

I have a large screen and i customed my win10 screen zooming on 125% , then i find that the function "ImageGrab" cannot work correctly,

What did you expect to happen?

I want get a screenshot of fullscreen, but it gave me part of it

What actually happened?

First I used default parameters,the function should have been given me a 1080p picture as my screen resolution, but only a 864p one, just topleft part of my screen. then i used some parameters in order to force set the function to give me screenshot on 1080p but the result is a picture seem like previous 864p picture with terrible black bar on right and below
it is obvious that 864p*125% =1080p, i think the reason caused this problem may concerned about the customed zooming of windows.

What are your OS, Python and Pillow versions?

  • OS: win10 x64 with 125% zooming
  • Python: 3.70
  • Pillow: 5.00

Origin code:

def scanScreen(tpl):
    capture=ImageGrab.grab()
    capture.save('capture.png') 
    pic=cv2.imread('capture.png')
    return findPosition(pic,tpl)

Code can work correctly

I find another way to get my screenshot by use WinAPI,it is fast and always work correctly, code is here, hope helpful

import win32gui, win32ui, win32con, win32api
def window_capture(filename):
  hwnd = 0 # window ID , '0' means the activated window 
  #get Divice Context
  hwndDC = win32gui.GetWindowDC(hwnd)
  #get mfcDC
  mfcDC = win32ui.CreateDCFromHandle(hwndDC)
  saveDC = mfcDC.CreateCompatibleDC()
  # create bigmap for picture saving
  saveBitMap = win32ui.CreateBitmap()
  # get Moniter info
  MoniterDev = win32api.EnumDisplayMonitors(None, None)
  w = MoniterDev[0][2][2]
  h = MoniterDev[0][2][3]
  # print w,h   #picture size
  # create bitmap space
  saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
  # save the screenshot to saveBitmap
  saveDC.SelectObject(saveBitMap)
  # from topleft(0,0)to bottomright(w,h)
  saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY)
  saveBitMap.SaveBitmapFile(saveDC, filename)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions