Displaying a xbm image

This is a discussion on Displaying a xbm image within the Xwindows forums, part of the Tools category; 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 = ...

Go Back   Unix Linux Forum > Technologies & Tools > Tools > Xwindows

FixUnix.com - Unix Linux Forums

Unix Content Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-15-2008, 12:32 AM
Default 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
Reply With Quote
  #2  
Old 09-18-2008, 12:31 AM
Default 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
Reply With Quote
  #3  
Old 09-18-2008, 05:52 AM
Default 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...

Reply With Quote
Reply

Thread Tools


All times are GMT -5. The time now is 12:55 AM.

In an effort to better serve ads to our visitors, cookies are used on Fixunix.com. For more information, check out our Privacy Policy.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
Ad Management by RedTyger