He entered the architecture school of the Royal Danish Academy of Art (Det Kongelige Danske Kunstakademi) at 12 years of age, but changed his course of study to painting at the age of 21. He began his training under Christoffer Wilhelm Eckersberg in 1828. He lost both his parents at about this time to typhus, and became the sole supporter of his younger sisters. He took over several commissions that had belonged to his father, including some copies to the Portrait Collection at Frederiksborg Palace and decorative paintings to Christiansborg Palace.

The Copenhagen Art Union (Kunstforening) commissioned a painting from Hansen in 1837. He provided them with "A Company of Danish Artists in Rome" (Et Selskab af danske Kunstnere i Rom). In addition he painted Italian folk scenes, and studies of Roman antiquities and architecture that reflect Eckersberg's spirit.

After eight years in Italy he finally returned to Denmark, staying briefly in Munich, where he studied the technique of fresco painting, in anticipation of a commission, along with Georg Hilker, to decorate the University of Copenhagen's vestibule on Frue Plads. This work continued from 1844 until 1853. Hansen painted the mythological figures, while Hilker painted the decorations and frameworks.

He married Magdalene Barbara Købke in 1846, and they had thirteen children. However, four children died within one year after being born and one of their sons, Hans Christian, was killed at the age of nineteen in a shipwreck.