When you see sentient beings troubled by suffering, you can make the following wish enthusiastically – from the depths of your heart – and with great force of will:
This person is suffering very badly, and despite wanting to gain happiness and alleviate suffering, does not know how to give up nonvirtues and adopt virtues. May his or her suffering, as well as its causes, ripen within me.

This is called the practice of taking the suffering of others within yourself using the instrument of compassion.

From the depths of your heart you can also imagine the following:
I will give to these sentient beings without the slightest hesitation or regret whatever virtues I have accumulated in the form of good karma, which will be auspicious for them.

This is called the practice of giving away your own happiness using the instrument of love.

Although such mental imagining may not actually bring about the desired results, it does increase determination and will power, while creating a peaceful atmosphere. These practices are performed in conjunction with the inhalation and exhalation of the breath – inhaling others' pain, and exhaling your own happiness into their lives.

Similarly, when you are ill, or suffer an unfortunate event, imagine the following:
May this illness or misfortune serve as a substitute for the suffering of all sentient beings.

This will keep your suffering from getting worse by fretting about it, and it will enhance your courage. It is also helpful to think:
May the suffering that I am undergoing now function as the ripening, manifestation, and conclusion of many bad karmas that I have accumulated.

According to Tibetan Buddhist texts on training in altruism, when you are happy, do not get too excited about it, but dedicate to the welfare of all sentient beings the good karma that yields happiness; and when you suffer, take on yourself all the pain of other sentient beings. We usually have ups and downs, but in this way you can maintain inner courage, not allowing either fortune or misfortune to disturb your peace of mind – neither too happy nor too sad, but stable

His Holiness the Dalai Lama in How to Be Compassionate