2
votes

I'm porting one of my existing projects over to GTK3+ (GTK-Sharp3 and such, since its a C# project). For the vast majority of it, it was a simple transition where not much needed to be changed but the way that I was doing a 'Region Screenshot' type system (wherein the user determines an area of a fullscreen image that they wish to save as a cropped version) seemed to have been deprecated with the new library (or at least I wasn't looking hard enough).

The code in question was the '[Pixmap].DrawPixBuf' method, wherein you'd be able to draw a pixbuf into a Pixmap with a number of parameters describing source 'rectangle' to take from, and 'destination' rectangle to place the contents of the source rectangle inside.

The code I was using originally was as follows:

pmBackBuffer.DrawPixbuf(drawingAreaRegion.Style.BaseGC(StateType.Normal),
pbHazy, 0, 0, 0, 0, platformSpecific.getScreenWidth(), platformSpecific.getScreenHeight(), RgbDither.Normal, 0, 0);

It takes pixmap 'pmBackBuffer', draws onto it with the BaseGC (a concept that also seems to be deprecated now), starting at the coordinates 0, 0 on the source, and placing them at the size of the current screen on the destination.

I gathered that maybe Cairo is the replacement for this type of system, and some documentation online suggested that the Pixmap object has been replaced with the Cairo surfaces, but I have no idea how to get a similar behaviour where I can draw a Pixbuf with those parameters onto the surface.

I also couldn't find anyone asking how to convert Pixmap to Surface. I found the other way around, but not the way I was looking for.

So the question, is there a method that works in a similar way to what I have above for the new Cairo surfaces?

Any help much appreciated.

1

1 Answers

2
votes

Turns out that the way to do this was first to create a Context, then use the

Gdk.CairoHelper

method

Gdk.CairoHelper.SetSourcePixbuf(Context, Pixbuf, 0, 0)

To transfer the Pixbuf into an existing ImageSurface. A more elaborated example is as follows:

private void Example(Pixbuf pb)
{
    ImageSurface imgSurface = new ImageSurface(Format.RGB24, pb.Width, pb.Height);

    using (Cairo.Context cr = new Cairo.Context(imgSurface)) {
        Gdk.CairoHelper.SetSourcePixbuf (cr, pb, 0, 0);
        cr.Paint ();
        cr.Dispose ();
    }
}