[cairo] What is the c# code to draw an image as a background then draw other stuff over it?

alan battersby alan.battersby at ntlworld.com
Thu Nov 12 12:37:43 PST 2009

My experience with cairo so far is just confined to drawing lines etc
and setting transformations. I have a gtk drawing area where I want to
draw a background image on it and then draw lines over that image.

I understand that I should be drawing the background onto an image
surface. I am assuming that in order to display this you should set the
context source property as shown in the DrawProfile() and then call
paint()!! is this correct. What do I then set the source to before
calling drawOutline(). When the code below runs everything seems to hang
up, I am very unsure about what is going on here so would be very
grateful for any information / suggestions / corrections.

Also when should I call the code to draw the grid, it needs to be after
the Gdk window is created but is there a signal to mark this event?


Example code

ImageSurface  _grid;		// grid drawn as an image


// called on expose event and when some properties are changed
void DrawProfile()
	if ((_profile == null) || (_profile.Count == 0)) return;
	ct = Gdk.CairoHelper.Create (this.GdkWindow);
	GdkWindow.Clear(); // is there a better way than this?
	****** do I need to set source back to original here? ****
	ct.Color = outlineColr;

// create grid image on imagesurface
// called once to create original image
// thereafter only when certain properties / window size etc change.
void drawGridToSurface()
	// Calculate desired size here.
	int width,height;
	if (GdkWindow == null)
	GdkWindow.GetSize(out width,out height);
	_grid = new ImageSurface(Format.ARGB32,width,height);
	Context cg = new Context(_grid);

// draw a profile over the grid image
protected void drawOutline(Context ct)
	double lw = 1.0, lh=1.0 , ld=5.0;
	ct.InverseTransformDistance(ref lw,ref lh);
	ct.LineWidth = Math.Min(lw,lh);
	ct.InverseTransformDistance(ref ld,ref lh);	
	double ld2 = ld / 2;
	bool first = true;

	foreach (float a in _profile.Profile.Keys) {
		float r = _profile.Value(a);
		double ar = Utilities.toRadians(a);
		float x = (float)(r * Math.Cos(ar));
		float y = (float)(r * Math.Sin(ar));
		if (first) {
			first = false;
		} else {
	foreach (float a in _profile.Profile.Keys) {
		float r = _profile.Value(a);
		double ar = Utilities.toRadians(a);
		float x = (float)(r * Math.Cos(ar));
		float y = (float)(r * Math.Sin(ar));

More information about the cairo mailing list