我試圖讓使用Xlib的和開羅截圖,但我不知道這樣做的好辦法,「步幅」實在是令人困惑我。製作使用的Xlib和開羅庫截圖[失效]
下面的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <cairo.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main(int argc, char** argv) {
int x, y;
Display *disp;
Window root;
XWindowAttributes watts;
XImage *image;
cairo_surface_t *surface;
unsigned int width;
unsigned int height;
int stride;
disp = XOpenDisplay(NULL);
root = DefaultRootWindow(disp);
XGetWindowAttributes(disp, root, &watts);
width = watts.width;
height = watts.height;
image = XGetImage(disp, root, watts.x, watts.y, width, height, AllPlanes, ZPixmap);
stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, width);
unsigned char *data = malloc(width * height * 3);
for (y = 0; y < height; ++y)
for (x = 0; x < width; ++x) {
unsigned long pixel = XGetPixel(image, x, y);
unsigned char red = (image->red_mask & pixel);
unsigned char green = (image->green_mask & pixel) >> 8;
unsigned char blue = (image->blue_mask & pixel) >> 16;
data[(y * width + x) * 3] = red;
data[(y * width + x) * 3 + 1] = green;
data[(y * width + x) * 3 + 2] = blue;
}
surface = cairo_image_surface_create_for_data(
data,
CAIRO_FORMAT_RGB24,
width, height,
stride);
cairo_surface_write_to_png(
surface,
"test.png");
cairo_surface_destroy(surface);
free(data);
return (EXIT_SUCCESS);
}
當我編譯並運行程序,一切似乎都工作得很好。然而,這裏的結果圖像:?
挺亂的權利.. 什麼我可能做錯了嗎?
你應該使用不同的指數計算,因爲像素大小是32位,而不是24查看更新。 – 2013-03-19 19:35:06
謝謝,這會生成屏幕截圖,但它都是綠色的 – user544262772 2013-03-19 19:35:49
這是因爲您沒有使用正確的班次和/或使用R,G和B的錯誤順序。 – 2013-03-19 19:37:25