Scanning electron microscope (SEM) based CD control and wafer inspection has an increasingly active role in the semiconductor industry. Current design rules require a CD control with a precision in the nanometer range. In order to achieve this precision, a complete model of the image formation mechanism is desirable. For this reason we present a three-dimensional simulation of SEM images. The simulations include Monte Carlo electron scattering, charging in the substrate and electron ray-tracing in the lower column. We demonstrate that in order to perform image formation it is necessary to include charging simulation. We investigate some specific cases in CD-SEM metrology: we will describe the effect of scan orientation relative to the orientation of the imaged feature on the apparent beam width, the effect of magnification on contact imaging, and the effect of residue in resist trenches. Our results, regarding these examples, clearly indicate that a fully three-dimensional numerical simulation is needed to obtain an understanding of image formation.