public Gdk.Rectangle ClampToImageSize(Gdk.Rectangle r)
{
int x = Utility.Clamp (r.X, 0, ImageSize.Width);
int y = Utility.Clamp (r.Y, 0, ImageSize.Height);
int width = Math.Min (r.Width, ImageSize.Width - x);
int height = Math.Min (r.Height, ImageSize.Height - y);
return new Gdk.Rectangle (x, y, width, height);
}