MUNICH — The teenage gunman who killed nine people in a rampage in Munich had been treated for depression and paranoia and appeared to have begun planning the attack about a year ago, the German authorities said on Sunday.

The gunman, Ali Sonboly, who was the 18-year-old son of Iranian immigrant parents and held both German and Iranian citizenship, had sought refuge in the internet, where he immersed himself in a violent video game and the so-called darknet of encrypted networks, through which the authorities suspect that he acquired the pistol used to carry out the attack.

Mr. Sonboly appeared to have begun planning the attack after visiting the site of a 2009 school shooting in Winnenden, Germany, in which 15 people were killed, initial evidence gleaned from his computer showed.

“We found a manifesto of his, in which he considers such attacks,” said Robert Heimberger, the chief of the Bavarian State Criminal Police. “From photos we found on a digital camera, we know that he visited the site and took pictures there.”