Displaying a xbm image - Xwindows

This is a discussion on Displaying a xbm image - Xwindows ; hi, I write a piece of code to display a xbm image onto a window, but it does not work. int main() { Display* display = XOpenDisplay(0); int screen = DefaultScreen(display); Window root = RootWindow(display, screen); Window wnd = ::XCreateSimpleWindow(display, ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Displaying a xbm image

  1. Displaying a xbm image

    hi, I write a piece of code to display a xbm image onto a window, but
    it does not work.

    int main()
    {
    Display* display = XOpenDisplay(0);
    int screen = DefaultScreen(display);
    Window root = RootWindow(display, screen);
    Window wnd = ::XCreateSimpleWindow(display, root,0, 0, 200, 200, 2,
    0, 0xffffff);
    GC gc = ::XCreateGC(display, wnd, 0, 0);
    std::cout<<"Window = "< &height, &bitmap, &x_hot, &y_hot))
    {
    std::cout<<"bitmap = "< "< int xcp = XCopyPlane(display, bitmap, wnd, gc, 0, 0, width, height,
    0,0, 1);
    std::cout<<"XCopyPlane = "< }

    ::XMapWindow(display, wnd);
    ::XFlush(display);

    ::sleep(5);
    }

    the program outputs XCopyPlane = 1, but the image is not printed on
    the window, I don't know the reason, could where am i wrong?

    Thanks in advance.
    Jinhao

  2. Re: Displaying a xbm image

    Jinhao wrote:
    > hi, I write a piece of code to display a xbm image onto a window, but
    > it does not work.
    >
    > int main()
    > {
    > Display* display = XOpenDisplay(0);


    I suggest using:

    if(NULL == display) {
    /* print some error message */
    }

    Otherwise the macros and calls below will segfault.

    > int screen = DefaultScreen(display);
    > Window root = RootWindow(display, screen);
    > Window wnd = ::XCreateSimpleWindow(display, root,0, 0, 200, 200, 2,
    > 0, 0xffffff);


    It's generally better to use BlackPixel() or WhitePixel() rather than
    hard coding 0xffffff.

    > GC gc = ::XCreateGC(display, wnd, 0, 0);
    > std::cout<<"Window = "<

    Check for BitmapSuccess - not 0.

    Also, you probably want to use wnd instead of root.

    > &height, &bitmap, &x_hot, &y_hot))
    > {
    > std::cout<<"bitmap = "< > "< > int xcp = XCopyPlane(display, bitmap, wnd, gc, 0, 0, width, height,
    > 0,0, 1);
    > std::cout<<"XCopyPlane = "< > }
    >
    > ::XMapWindow(display, wnd);
    > ::XFlush(display);
    >
    > ::sleep(5);


    You're missing an integer to return from main. You could #include
    and return EXIT_SUCCESS, or simply return 0;

    It would help to turn on -Wall if you're using gcc.

    > }
    >
    > the program outputs XCopyPlane = 1, but the image is not printed on
    > the window, I don't know the reason, could where am i wrong?
    >


    You need to keep the bitmap around until you receive an Expose event
    (after you map the window), and then XCopyPlane.

    My tutorial may help:
    http://www.xmission.com/~georgeps/do...eginner-7.html


    George

  3. Re: Displaying a xbm image

    On 9月18日, 下午12时31分, GPS wrote:
    > Jinhao wrote:
    > > hi, I write a piece of code to display a xbm image onto a window, but
    > > it does not work.

    >
    > > int main()
    > > {
    > > Display* display = XOpenDisplay(0);

    >
    > I suggest using:
    >
    > if(NULL == display) {
    > /* print some error message */
    > }
    >
    > Otherwise the macros and calls below will segfault.
    >
    > > int screen = DefaultScreen(display);
    > > Window root = RootWindow(display, screen);
    > > Window wnd = ::XCreateSimpleWindow(display, root,0, 0, 200, 200, 2,
    > > 0, 0xffffff);

    >
    > It's generally better to use BlackPixel() or WhitePixel() rather than
    > hard coding 0xffffff.
    >
    > > GC gc = ::XCreateGC(display, wnd, 0, 0);
    > > std::cout<<"Window = "<
    > > Pixmap bitmap;
    > > unsigned width, height;
    > > int x_hot, y_hot;
    > > if(0 == XReadBitmapFile(display, root, "Test.xbm", &width,

    >
    > Check for BitmapSuccess - not 0.
    >
    > Also, you probably want to use wnd instead of root.
    >
    > > &height, &bitmap, &x_hot, &y_hot))
    > > {
    > > std::cout<<"bitmap = "< > > "< > > int xcp = XCopyPlane(display, bitmap, wnd, gc, 0, 0, width, height,
    > > 0,0, 1);
    > > std::cout<<"XCopyPlane = "< > > }

    >
    > > ::XMapWindow(display, wnd);
    > > ::XFlush(display);

    >
    > > ::sleep(5);

    >
    > You're missing an integer to return from main. You could #include
    > and return EXIT_SUCCESS, or simply return 0;
    >
    > It would help to turn on -Wall if you're using gcc.
    >
    > > }

    >
    > > the program outputs XCopyPlane = 1, but the image is not printed on
    > > the window, I don't know the reason, could where am i wrong?

    >
    > You need to keep the bitmap around until you receive an Expose event
    > (after you map the window), and then XCopyPlane.
    >
    > My tutorial may help:http://www.xmission.com/~georgeps/do...ls/Xlib_Beginn...
    >
    > George


    Thanks for your replay. I modified my program.

    int main()
    {
    Display* display = XOpenDisplay(NULL);
    if(display == NULL)
    {
    return 0;
    }

    int screen = DefaultScreen(display);
    Window root = RootWindow(display, screen);
    Window wnd = ::XCreateSimpleWindow(display, root,0, 0, 200, 200, 2,
    0, 0xffffff);
    XSelectInput(display, wnd, ExposureMask);
    GC gc = ::XCreateGC(display, wnd, 0, 0);

    Pixmap bitmap;
    unsigned width, height;
    int x_hot, y_hot;
    if(BitmapSuccess != XReadBitmapFile(display, wnd, "Test.xbm", &width,
    &height, &bitmap, &x_hot, &y_hot))
    {
    return 0;
    }

    ::XMapWindow(display, wnd);
    ::XFlush(display);

    while(true)
    {
    XEvent evt;
    XNextEvent(display, &evt);
    if(evt.type == Expose)
    {
    std::cout<<"Expose"< int xcp = XCopyPlane(display, bitmap, wnd, gc, 0, 0, width, height,
    0,0, 1);
    std::cout<<"XCopyPlane = "< XFlush(display);
    }
    }

    return 0;
    }

    The program prints
    Expose
    XCopyPlane = 1

    that means the program is right? but the window displays a black
    rectangle...


  4. Re: Displaying a xbm image

    I've got the same problem, guys, and I am getting the same black screen?
    Did you usenet resolve this problem?
    If so, please inform me as soon as possible.
    thanks.