来自 WinAPI 的 PIL 和位图
我有使用 winapi 制作窗口屏幕截图的代码。然后我必须将图像保存到磁盘并再次从磁盘加载到内存 PIL。有没有办法立即在不保存到磁盘的情况下在 PIL 中传递此位图。
import win32gui, win32ui, win32con
import Image
win_name='Book'
bmpfilenamename='1.bmp'
hWnd = win32gui.FindWindow(None, win_name)
windowcor = win32gui.GetWindowRect(hWnd)
w=windowcor[2]-windowcor[0]
h=windowcor[3]-windowcor[1]
wDC = win32gui.GetWindowDC(hWnd)
dcObj=win32ui.CreateDCFromHandle(wDC)
cDC=dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0,0),(w, h) , dcObj, (0,0), win32con.SRCCOPY)
dataBitMap.SaveBitmapFile(cDC, bmpfilenamename)
#dcObj.DeleteDC()
#cDC.DeleteDC()
#win32gui.ReleaseDC(hWnd, wDC)
im=Image.open(bmpfilenamename)
im.load()
I have code that makes a screenshot of the window using winapi. Then I have to save image to disk and load it again from disk to memory PIL. Is there any way at once without saving to disk to pass this bitmap in the PIL.
import win32gui, win32ui, win32con
import Image
win_name='Book'
bmpfilenamename='1.bmp'
hWnd = win32gui.FindWindow(None, win_name)
windowcor = win32gui.GetWindowRect(hWnd)
w=windowcor[2]-windowcor[0]
h=windowcor[3]-windowcor[1]
wDC = win32gui.GetWindowDC(hWnd)
dcObj=win32ui.CreateDCFromHandle(wDC)
cDC=dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0,0),(w, h) , dcObj, (0,0), win32con.SRCCOPY)
dataBitMap.SaveBitmapFile(cDC, bmpfilenamename)
#dcObj.DeleteDC()
#cDC.DeleteDC()
#win32gui.ReleaseDC(hWnd, wDC)
im=Image.open(bmpfilenamename)
im.load()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注释掉这一行:
并添加以下内容:
[来自 另一个问题]
Comment out this line:
and add this instead:
[from another SO question ]
如果您正在使用 PIL 为什么不使用它来截图?
PIL 包含 ImageGrab 模块,可以像这样使用:
但是,只有当您的应用程序位于前台时,它才会获得正确的屏幕截图。
If you are using PIL why not to use it for screenshoting?
PIL contains ImageGrab module, which can be used like:
However it will get correct screenshot only if your app is foreground.