Hello all,

Anyone know how to draw the widgets contained in a Fl_Scroll into an image 
file? I have searched the fltk.general and found this thread:

http://www.fltk.org/newsgroups.php?s1+gfltk.general+v39+T+Q"capture+widget+image";

I've tried manipulating the example for my needs, but it's not going too 
well. :)

I have pasted my implementation so far at the bottom of this message.

My idea/hopes is to have a button that will capture the contents of the 
Fl_Scroll and save a PNG image. I have the saving part implemented and test 
independent of this problem.

When the button is pushed, the capture() method (see below) is called and 
returns a Fl_RGB_Image. I then write the image data to a PNG file.

I think my biggest problem is that I don't fully understand the Fl_Offscreen 
and its associated functions. I have searched the HTML doxygen docs, but 
documentation for Fl_Offscreen, fl_begin_offscreen, etc. are missing.

The image I am getting in the end is a jumbled mess of the "Hello World" 
string and random stuff from my desktop.

Any tips or direction to some archived threads would be greatly appreciated.

Thanks,

Alvin



Fl_RGB_Image* Fl_Canvas::capture()
{
   if(children() <= 2)
      return NULL;

   int xdim = child(0)->w();
   int ydim = child(0)->h();

   window()->make_current();

   // setup the offscreen buffer
   Fl_Offscreen offscr = fl_create_offscreen(xdim, ydim);

   // force children to draw into the offscreen buffer
   fl_begin_offscreen(offscr);
      Fl_Widget *o = NULL;

      for(int i = children()-1; i >= 0; --i)
      {
         o = child(i);

         if(o != &scrollbar && o != &hscrollbar)
            o->draw();
      }
        
      fl_rectf(10, 10, 150, 100, FL_BLACK);
      fl_color(FL_WHITE);
      fl_font(FL_TIMES, 15);
      fl_draw("Hello, World!", 15, 10);
   fl_end_offscreen();

   // copy??
   fl_copy_offscreen(0, 0, xdim, ydim, offscr, 0, 0);

   // copy the offscreen buffer into a RGBA buffer
   uchar *offscreenImage = new uchar[xdim * ydim * 4];
   memset(offscreenImage, 0, xdim * ydim * 4);

   fl_read_image(offscreenImage, x(), y(), xdim, ydim, 255);

   // Pack a Fl_RGB_Image and force its dtor to do the cleanup
   Fl_RGB_Image *rgb_img = new Fl_RGB_Image(offscreenImage, xdim, ydim, 4);
   rgb_img->alloc_array = 1;

   return rgb_img;
}

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to