簡體   English   中英

XGetImage 中的 Xlib 圖像抓取失敗

[英]Xlib image grab fails in XGetImage

Cookie 切割(從所謂的工作代碼中)一個簡單的 C 程序使用 XGetImage() 執行 Xlib 圖像抓取。 在這一點上,我並沒有嘗試處理圖像,這只是一個概念驗證,看看圖像抓取是否有效——但它沒有。 XGetImage() 調用失敗,如:

X Error of failed request:  BadMatch (invalid parameter attributes)
  Major opcode of failed request:  73 (X_GetImage)
  Serial number of failed request:  21
  Current serial number in output stream:  21

我花了相當多的時間研究這個問題,顯然這個問題一直困擾着其他開發人員,並且沒有得到明確的答案。 有人知道我如何解決這個問題嗎? 我可以從 printf 中看出,感興趣的 window 被正確識別。 XMapRaised() 是先前線程對此問題的建議,但似乎沒有幫助。 這是代碼:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "/usr/include/X11/Xlib.h"

Window findScidWindow(Display *display )
{
    Bool found = False;
    Window rootWindow = RootWindow(display, DefaultScreen(display));
    Atom atom = XInternAtom(display, "_NET_CLIENT_LIST", True);
    Atom actualType;
    int format;
    unsigned long numItems;
    unsigned long bytesAfter;

    unsigned char *data = '\0';
    Window *list;
    char *windowName;

    int status = XGetWindowProperty(display, rootWindow, atom, 0L, (~0L), False,
        AnyPropertyType, &actualType, &format, &numItems, &bytesAfter, &data);
    list = (Window *)data;

    if (status >= Success && numItems)
    {
        for (int i = 0; i < numItems; ++i)
        {
            status = XFetchName(display, list[i], &windowName);
            if (status >= Success)
            {
                if(strstr(windowName, "Scid vs. PC") != NULL)
                {
                    XFree(windowName);
                    XFree(data);
                    return list[i];
                }
            }
        }
    }
}
void
main( int argc, char*argv )
{
    Display* d = XOpenDisplay(":0.0");
    XImage *image;

    Window root = (Window)0x0560003b; /* obtained via 'wmctrl -l -G' */

    Window ScidWindow = findScidWindow(d);
    XWindowAttributes attrib;

    XGetWindowAttributes(d, ScidWindow, &attrib);

    int width = attrib.width;
    int height = attrib.height;
printf("width: %d    height: %d\n",width,height);

    XMapRaised(d, root);

    /* coordinates 438,110 obtained via 'wmctrl -l -G' */
    image = XGetImage( d, ScidWindow, 438, 110, width, height, AllPlanes, ZPixmap);
}

問題是

    image = XGetImage( d, ScidWindow, 438, 110, width, height, AllPlanes, ZPixmap);

使用x = 438y = 110如果x + width實際上大於 window 寬度(高度相同),這將是一個特別的問題

所以在這里我必須假設你不是試圖裁剪 window 圖像,而是想要拍攝一個普通的原始截圖,那么你只需要為 x 和 y 傳遞 0:

    image = XGetImage( d, ScidWindow, 0, 0, width, height, AllPlanes, ZPixmap);

解釋是坐標系不是全屏,而是你抓的window之一。 表示 window 從 (0, 0) 開始。

這也花了我一些時間來弄清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM