Reputation: 2289
I have a fine, working system in C# that draws with Cairo commands in a render method. However, sometimes I would like to draw into a pixmap, rather than dynamically when the screen needs to be updated. For example, currently I have:
public override void render(Cairo.Context g) {
g.Save();
g.Translate(x, y);
g.Rotate(_rotation);
g.Scale(_scaleFactor, _scaleFactor);
g.Scale(1.0, ((double)_yRadius)/((double)_xRadius));
g.LineWidth = border;
g.Arc(x1, y2, _xRadius, 0.0, 2.0 * Math.PI);
g.ClosePath();
}
But I would like, if I choose, to render the Cairo commands to a Gtk.Pixbuf. Something like:
g = GetContextFromPixbuf(pixbuf);
render(g);
Is that possible? It would be great if I didn't have to turn the context back into a pixbuf, but that the cairo drawing would go directly to the pixbuf. Any hints on this would be appreciated!
Upvotes: 2
Views: 2234
Reputation: 2289
The answer is actually quite easy: when you render the objects, render them to a context created from a saved surface. Then when you render the window, insert a context based on the same saved surface.
Create a surface:
surface = new Cairo.ImageSurface(Cairo.Format.Argb32, width, height);
Render a shape to the surface:
using (Cairo.Context g = new Cairo.Context(surface)) {
shape.render(g); // Cairo drawing commands
}
Render the window:
g.Save();
g.SetSourceSurface(surface, 0, 0);
g.Paint();
g.Restore();
... // other Cairo drawing commands
That's it!
Upvotes: 1