We present an analytic solution for the magnetic field of a bar-shaped permanent magnet. Assuming a constant magnetization, we derive expressions for the stray field in three dimensions. The analytic solutions can be readily applied to field calculation problems for magnetic force microscopy simulations without the need for finite element methods.