The nano-X API contains a function, GrDrawImageFromFile(), which will read images from a file and draw the image onto a window or pixmap. Multiple image formats (GIF, JPEG, BMP, PNG, XPM, PBM, PGM and PPM) are supported by GrDrawImageFromFile(). The image type is automatically determined when the file is read.
The image file must reside within the nano-X server's file system. The client application just passes the filename to the server then the server reads the file. This is no problem as long as the the client and server are on the same machine. Another thing to be aware of is that since the server is opening the file, all relative paths in the image file name are relative to the nano-X server's current directory rather than the client's current working directory.
The following example shows how to display Tux as an image loaded from file at run time. The file is read each time an exposure event is received. This approach is rather slow, in the next example we will look at a method to read the file once into memory and draw from memory during the exposure event.
Create a directory in which to build this example. Then copy the bitmap image of Tux from the Microwindows sources to this directory. Also copy the example source shown below into a file named "image_02.c".
![]() | In this example assume that Microwindows is installed to ~/microwin and the examples are built in ~/mymw/ex_image_02. If you have different locations then you will have to modify the paths used in the example accordingly. |
$ cd ~/mymw/ex_image_02 $ cp ~/microwin/src/mwin/bmp/penguin.bmp penguin.bmp |
Compile the application with the following command.
![]() | If you did not install Microwindows you will need to change the path /usr/include/microwin so that it points to the include directory where you extracted the Microwindows source. |
$ gcc image_02.c \ > -I/usr/include/microwin \ > -o image_02 -lnano-X |
Example 2-2. image_02.c
#include <stdio.h> #define MWINCLUDECOLORS #include "microwin/nano-X.h" GR_WINDOW_ID wid; GR_GC_ID gc; void event_handler (GR_EVENT *event); int main (void) { if (GrOpen() < 0) { fprintf (stderr, "GrOpen failed"); exit (1); } gc = GrNewGC(); wid = GrNewWindowEx (GR_WM_PROPS_APPFRAME | GR_WM_PROPS_CAPTION | GR_WM_PROPS_CLOSEBOX, "Tux Window II", GR_ROOT_WINDOW_ID, 50, 50, 100, 200, WHITE); GrSelectEvents (wid, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_CLOSE_REQ); GrMapWindow (wid); GrMainLoop (event_handler); return 0; } void event_handler (GR_EVENT *event) { switch (event->type) { case GR_EVENT_TYPE_EXPOSURE: { GR_WINDOW_INFO info; GrGetWindowInfo (wid, &info); GrDrawImageFromFile (wid, gc, 0, 0, info.width, info.height, "penguin.bmp", 0); break; } case GR_EVENT_TYPE_CLOSE_REQ: GrClose(); exit (0); } } |
Run the example application with the following command.
$ nano-X& sleep 1; nanowm& sleep 1; ./image_02& |