2013-02-21 133 views
2

我嘗試使用bmp文件在x11窗口中設置背景圖像。我使用XReadBitmapFile但它不工作。我如何使用bmp文件來設置x11窗口背景。在此先感謝如何使用x11窗口背景加載bmp文件

+0

你能_show_我們你試過嗎? – 2013-02-21 05:21:06

+0

GC gc = XCreateGC(d,w,0,NULL); XCopyPlane(d,bitmap,w,gc,0,0,width,height,0,0,1)int resource = XReadBitmapFile(d,w,「demo.bmp」,&width,&height,&bitmap,&x,&y); ); – VigneshK 2013-02-21 06:42:57

回答

2

XReadBitmapFile僅用於讀取.xbm文件。我們需要的是一個用於讀取BMP文件的庫,一種可能性是ImLib2,它可以讀取多種類型的文件,並且可以很好地與Xlib配合使用。

下面是使用它的一個稍長的例子:

/* displays an image or sets root background 
* PUBLIC DOMAIN - CC0 http://creativecommons.org/publicdomain/zero/1.0/ 
* J.Mayo 2013 
* 
* gcc -Wall -W -g3 -o xrootbg xrootbg.c -lX11 -lImlib2 
* 
*/ 
#include <stdio.h> 
#include <X11/Xlib.h> 
#include <Imlib2.h> 

int main(int argc, char **argv) 
{ 
    Imlib_Image img; 
    Display *dpy; 
    Pixmap pix; 
    Window root; 
    Screen *scn; 
    int width, height; 
    const char *filename = NULL; 

    if (argc < 2) 
     goto usage; 
    filename = argv[1]; 

    img = imlib_load_image(filename); 
    if (!img) { 
     fprintf(stderr, "%s:Unable to load image\n", filename); 
     goto usage; 
    } 
    imlib_context_set_image(img); 
    width = imlib_image_get_width(); 
    height = imlib_image_get_height(); 

    dpy = XOpenDisplay(NULL); 
    if (!dpy) 
     return 0; 
    scn = DefaultScreenOfDisplay(dpy); 
    root = DefaultRootWindow(dpy); 

    pix = XCreatePixmap(dpy, root, width, height, 
     DefaultDepthOfScreen(scn)); 

    imlib_context_set_display(dpy); 
    imlib_context_set_visual(DefaultVisualOfScreen(scn)); 
    imlib_context_set_colormap(DefaultColormapOfScreen(scn)); 
    imlib_context_set_drawable(pix); 

    imlib_render_image_on_drawable(0, 0); 
    XSetWindowBackgroundPixmap(dpy, root, pix); 
    XClearWindow(dpy, root); 

    while (XPending(dpy)) { 
     XEvent ev; 
     XNextEvent(dpy, &ev); 
    } 
    XFreePixmap(dpy, pix); 
    imlib_free_image(); 
    XCloseDisplay(dpy); 
    return 0; 
usage: 
    fprintf(stderr, "usage: %s <image_file>\n", argv[0]); 
    return 1; 
} 
+0

現在它的工作謝謝。是否有任何「Imlib2」手冊知道Imlib2中的各種API – VigneshK 2013-02-22 04:18:06

+0

[link](http://docs.enlightenment.org/api/imlib2/html/)有完整的文檔和一些有用的例子Imlib2只是衆多可用的庫中的一個,它們的工作原理大致相同,但其他人可能需要使用XImage而不是爲您進行渲染,但XImage非常簡單,並且是標準xlib的一部分。 – orangetide 2013-02-26 05:23:53