The fundamental possibility of measuring the stray fields distributions of magnetic objects in a scanning electron microscope is demonstrated. The main idea of the method is to use a sample holder for operation in the mode of a scanning transmission microscope, additionally equipped with a square diffraction grating. This grating, formed on the basis of a metal film, is placed at some distance under the magnetic sample. Reconstruction of the deflections of transmitted electrons by analyzing the geometric phase from visible distortions of the diffraction grating made it possible to reconstruct the distribution of the magnetic field created by the sample under study.