16

對於this project,我使用Windows API截圖(處理多屏幕)並將其轉換爲PIL圖像;那麼如果需要,我會在窗戶周圍添加一個陰影。Python PIL和Windows API的活動窗口截圖:如何處理圓角?

我的問題是,截圖實際上是窗口的矩形;這意味着我捕捉到它背後的背景,並且我不想那樣。我搜索了一下,找到了關於透明度的文檔和內容,我猜我應該找到一種方法來獲得窗口的形狀,以便使它成爲我將應用於(矩形)圖像的遮罩。得到。但我現在發現要拿到那個面具。任何人都可以幫忙嗎?

下面是我的代碼:

hwnd = win32gui.GetForegroundWindow() 

l, t, r, b = win32gui.GetWindowRect(hwnd) 
w = r - l 
h = b - t 

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, (0, 0), win32con.SRCCOPY) 

#add cursor 
if showcursor: 
    curFlags, curH, (curX, curY) = win32gui.GetCursorInfo() 
    saveDC.DrawIcon((curX, curY), curH) 

#load into PIL image 
"""http://stackoverflow.com/questions/4199497/image-frombuffer-with-16-bit-image-data""" 
bmpinfo = saveBitMap.GetInfo() 
bmpstr = saveBitMap.GetBitmapBits(True) 
im = Image.frombuffer(
    'RGB', 
    (bmpinfo['bmWidth'], bmpinfo['bmHeight']), 
    bmpstr, 'raw', 'BGRX', 0, 1) 

win32gui.DeleteObject(saveBitMap.GetHandle()) 
saveDC.DeleteDC() 
mfcDC.DeleteDC() 
win32gui.ReleaseDC(hwnd, hwndDC) 

return im 

下面是一個藍色的背景上方窗口的稍微放大截圖:

正如你可以看到有藍色邊角該不該」不要在那裏。

+0

我仍然找不到解決方案。任何人? – NorthernLights 2011-05-22 02:32:56

+3

事實上,我從來沒有發現成功的軟件處理窗口捕獲,即使是由MicroSoft提供的SnippingTool.exe。嘗試剪切2個像素,或製作2個純黑色/白色像素。如果你想切割完全的形狀,閱讀MS文檔:窗口角形狀在system.RadiusX/RadiusY中定義http://msdn.microsoft.com/en-us/library/system.windows.shapes.rectangle.radiusy.aspx #Y0 – fanlix 2012-05-24 13:42:33

+0

也有類似的問題? http://stackoverflow.com/questions/2224220/win32-how-to-make-drop-shadow-honor-non-rectangular-layered-window – 2012-06-16 19:25:56

回答

1

爲什麼不使用Edge detection算法(f.e. Prewitt或Sobel)來檢測窗口邊緣,只需要將Alpha通道設置爲圖像限制和窗口邊緣限制之間的像素即可。