首先安装 libimlib2-dev 软件包,
# sudo apt install libimlib2-dev
然后安装 giblib
# wget http://pkgs.[Fedora](http://www.linuxidc.com/topicnews.aspx?tid=5 "Fedora")project.org/repo/pkgs/giblib/giblib-1.2.4.tar.gz/c810ef5389baf24882a1caca2954385e/giblib-1.2.4.tar.gz
# tar zxvf giblib-1.2.4.tar.gz
# cd giblib-1.2.4
# ./configure
# make
# sudo make install
最后编写源码文件 Makefile、screen.c 和 screen.h ,并执行 make 命令编译。
Makefile:
CC = gcc
CFLAGS = -Wall -Werror -Wcast-align -g
LDFLAGS =
SCREENOBJECT = screen.o
GIBLIBCFLAGS = `giblib-config --cflags`
GIBLIBLIBS = `giblib-config --libs`
all: screen
screen: $(SCREENOBJECT)
$(CC) $(LDFLAGS) $(GIBLIBLIBS) $(SCREENOBJECT) -lX11 -o $@
screen.o: screen.c screen.h
$(CC) $(CFLAGS) $(GIBLIBCFLAGS) -c $<
clean:
rm -f screen *~ *.o a.out
clean-media:
rm -f *~ *.jpeg
.PHONY: all clean
screen.c:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <giblib/giblib.h>
#include "screen.h"
void
screen_init(struct screen *screen)
{
screen->display = XOpenDisplay(NULL);
if (!screen->display) {
perror("XOpenDisplay");
exit(EXIT_FAILURE);
}
screen->screen = ScreenOfDisplay(screen->display,
DefaultScreen(screen->display));
screen->visual = DefaultVisual(screen->display,
DefaultScreen(screen->display));
screen->colormap = DefaultColormap(screen->display,
DefaultScreen(screen->display));
screen->root = RootWindow(screen->display,
DefaultScreen(screen->display));
imlib_context_set_display(screen->display);
imlib_context_set_visual(screen->visual);
imlib_context_set_colormap(screen->colormap);
}
int
main(int argc, char *argv[])
{
struct screen screen;
Imlib_Image image;
screen_init(&screen);
image = gib_imlib_create_image_from_drawable(screen.root,
0,
0,
0,
screen.screen->width,
screen.screen->height,
1);
imlib_context_set_image(image);
imlib_image_set_format("jpeg");
imlib_save_image("screen.jpeg");
imlib_free_image();
exit(EXIT_SUCCESS);
}
screen.h:
#ifndef SCREEN_H
#define SCREEN_H
#include <X11/Xlib.h>
struct screen {
Display *display;
Screen *screen;
Visual *visual;
Colormap colormap;
Window root;
};
void screen_init(struct screen *screen);
#endif
网友评论