Re: Using Cairo and getting data back out.



That works brilliantly.  Thank you very much. I never would have
gotten to that one on my own, don't understand the internals enough.
:-)

- John Hobbs

john velvetcache org

On Tue, Aug 12, 2008 at 12:15 AM, Chris Wilson
<christopher g wilson gmail com> wrote:
> Here is the method I successfully use:
>
> I have a class called daViewMap which is derived from Gtk::DrawingArea
> I use a Cairo::ImageSurface to import a 2-D array into an useful image form
> for Cairo.
>
> class daViewMap : public Gtk::DrawingArea
> {
> public:
>      Cairo::RefPtr<Cairo::ImageSurface> viewSurface;
>      Cairo::RefPtr<Cairo::Context> myCr;
> };
>
> daViewMap::daViewMap() :
> {
>     viewSurface = Cairo::ImageSurface::create(&viewMap[0][0],
> Cairo::FORMAT_A8, VIEW_MAP_X, VIEW_MAP_Y, VIEW_MAP_X );
>     myCr = Cairo::Context::create(viewSurface);
> }
>
> I perform all the cairo-based drawing using the context daViewMap->myCr
> and then extract the image via a button callback:
>
> void exportImage(std::string filename)
> {
>         Glib::RefPtr<Gdk::Window> win2 = daViewMap->get_window();
>         if (win2)
>         {
>             Glib::RefPtr<Gdk::Drawable> dra2 =
> Glib::RefPtr<Gdk::Window>::cast_dynamic(win2);
>             Glib::RefPtr<Gdk::Pixbuf> img2 = Gdk::Pixbuf::create(dra2 ,0 ,0
> ,daViewMap->get_allocation().get_width(),
> daViewMap->get_allocation().get_height());
>             img2->save(filename,Glib::ustring("png"));
>         }
> }
>
> Note the use of a Pixbuf to extract the image from the drawing area's space,
> and then we use that to save it out to a file.
>
> Hope that makes sense...
> Chris Wilson
>
> On Mon, Aug 11, 2008 at 5:48 PM, John Hobbs <john velvetcache org> wrote:
>>
>> Hello, I apologize if this is off-topic as it deals a lot with Cairo,
>> I feel it is a borderline thing.  Please feel free to send me packing
>> to somewhere else if I'm wrong on that point :-)
>>
>> I'm trying to load an image into a DrawingArea, then put lines on it
>> with Cairo and get that back out.  Is this possible or do I need to go
>> figure out something with GC and Drawable?
>>
>> Here's the guts of the procedure so far, sorry it's big but I don't
>> know how to trim it down and keep it understandable.
>>
>> ---------------------------------------------
>> class ExampleWindow : public Gtk::Window {
>>
>>        public:
>>                ExampleWindow (std::string path) {
>>                        imagePB = Gdk::Pixbuf::create_from_file(path);
>>
>>  drawOnMe.signal_expose_event().connect(sigc::mem_fun(*this,&ExampleWindow::on_da_expose_event));
>>                        add(drawOnMe);
>>                        show_all();
>>                }
>>
>>        private:
>>                Gtk::DrawingArea drawOnMe;
>>                Glib::RefPtr<Gdk::Pixbuf> imagePB;
>>
>>                bool on_da_expose_event (GdkEventExpose * event) {
>>                        std::cout << "Expose Event" << std::endl;
>>                        Glib::RefPtr<Gdk::Window> window =
>> drawOnMe.get_window();
>>                        Cairo::RefPtr<Cairo::Context> context =
>> window->create_cairo_context();
>>                        Cairo::RefPtr<Cairo::ImageSurface> image_surface =
>> Cairo::ImageSurface::create (Cairo::FORMAT_RGB24,
>> imagePB->get_width(), imagePB->get_height());
>>                        Cairo::RefPtr<Cairo::Context> image_context =
>> Cairo::Context::create(image_surface);
>>
>>                        Gdk::Cairo::set_source_pixbuf (image_context,
>> imagePB, 0.0, 0.0);
>>                        image_context->paint();
>>
>>                        context->set_source(image_surface, 0.0, 0.0);
>>                        context->paint();
>>
>>                        context->set_line_width(5.0);
>>                        context->set_source_rgb(0.8, 0.0, 0.0);
>>                        context->move_to(10,20);
>>                        context->line_to(100, 200);
>>                        context->stroke();
>>
>>                        return false;
>>                }
>> };
>> ---------------------------------------------
>>
>> I tried getting a surface from that context after the stroke() call
>> and then a write to PNG:
>>
>> Cairo::RefPtr<Cairo::Surface> sfc = context->get_target();
>> sfc->write_to_png("please_work.png");
>>
>> That just crashed on an expose event and I didn't bother to debug. I
>> have the feeling I've gone wrong in my understanding of Cairo.
>>
>> So I guess what it boils down to is I'm curious if there is an easier
>> path by using something in Gtkmm (which I chased my tail for a long
>> time trying to find something) or if there is a way to get that drawn
>> data back out of there.
>>
>> Thanks for any and all help :-)
>>
>> - John Hobbs
>>
>> john velvetcache org
>> _______________________________________________
>> gtkmm-list mailing list
>> gtkmm-list gnome org
>> http://mail.gnome.org/mailman/listinfo/gtkmm-list
>
>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]