[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


Hi,
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?

thanks
Alan

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?
	ct.SetSource(_grid);
	ct.Paint();
	****** do I need to set source back to original here? ****
	ct.Save();
	ct.Color = outlineColr;
	drawOutline(ct);
	ct.Restore();
	
}

// 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)
		return;
	GdkWindow.GetSize(out width,out height);
	_grid = new ImageSurface(Format.ARGB32,width,height);
	Context cg = new Context(_grid);
	scaleView(cg);
	drawCentre(cg);
	drawGrid(cg);
}

// 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) {
			ct.MoveTo(x,y);
			first = false;
		} else {
			ct.LineTo(x,y);
		}
	}
	ct.Stroke();
	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));
		ct.Rectangle(x-ld2,y-ld2,ld,ld);
		ct.Stroke();
	}
			
}
		



More information about the cairo mailing list