Theory of magnetic imaging by force microscopy