Draw Images
Prev | Chapter 14. The Drawing Area Widget | Next

## Draw Images

There are a couple of drawing methods for putting image data into a drawing area. `draw_pixmap()` can copy the contents of a `Gdk::Drawable` (the window of a drawing area is one) into the drawing area. There is also `draw_bitmap()` for drawing a two-color image into the drawing area, and `draw_image()` for drawing an image with more than two colors.

For all of these methods, the first argument is the `Gdk::GC`. The second argument is the object of the appropriate type to copy in: `Gdk::Drawable`, `Gdk::Bitmap`, `Gdk::Image`. The next two arguments are the x and y points in the image to begin copying from. Then come the x and y points in the drawing area to copy to. The final two arguments are the width and heigh of the area to copy.

There is also a method for drawing from a `Gdk::Pixbuf`. A `Gdk::Pixbuf` buffer is a useful wrapper around a collection of pixels, which can be read from files, and manipulated in various ways.

Probably the most common way of creating `Gdk::Pixbuf`s is to use `Gdk::Pixbuf::create_from_file()`, which can read an image file, such as a png file into a pixbuf ready for rendering.

The `Gdk::Pixbuf` can be rendered with `render_to_drawable`, which takes quite a few parameters. The `render_to_drawable` is a member of `Gdk::Pixbuf` rather than `Gdk::Drawable`, which is unlike the `draw_*` functions described earlier. As such, its first parameter is the drawable to render to. The second parameter is still the `Gdk::GC`. The next two parameters are the point in the pixbuf to start drawing from. This is followed by the point in the drawable to draw it at, and by the width and height to actually draw (which may not be the whole image, especially if you're only responding to an expose event for part of the window). Finally, there are the dithering parameters. If you use Gdk::RGB_DITHER_NONE as the dither type, then the dither offset parameters can both be 0.

Here is a small bit of code to tie it all together: (Note that usually you wouldn't load the image every time in the expose event handler! It's just shown here to keep it all together)

    bool myarea::on_expose_event(GdkEventExpose* ev)
    {
      Glib::RefPtr<Gdk::PixBuf> image = Gdk::PixBuf::create_from_file("myimage.png");
      image->render_to_drawable(get_window(), get_style()->get_black_gc(),
        0, 0, 100, 80, image->get_width(), image->get_height(), // draw the whole image (from 0,0 to the full width,height) at 100,80 in the window
        Gdk::RGB_DITHER_NONE, 0, 0);
      return true;
    }

Prev | Up | Next
Drawing Text | Home | Chapter 15. Drag and Drop