Fiqh-us-Sunnah Volume 4, Funerals and Dhikr, Fiqh 4.126A.

Section : On Seeing Someone in Distress.

Abu Hurairah reported that the Prophet, peace be upon him, said: “If one sees an afflicted person and says, ‘Praise and thanks be to Allah Who has saved me from what he has afflicted you with, and has honoured me over many of His creatures,’ he will be saved from that affliction.” (Reported by Tirmizhi, who considers it sound)

An-Nawawi states that the scholars said, “One should say the above mentioned supplication inaudibly so that the afflicted person should not hear it, lest he should be grieved by it. But if the affliction is the result of his sinful conduct then there is no harm in his listening if he is not heedful of evil.”