At the end of two full years, Paroh dreamed, and, behold, he was standing by the river.
The word וַיְהִי usually denotes a painful episode, as Chazal teach us. The Ohr Hachaim explains that the golus of Mitzrayim started in this week’s Parsha.
The truth is, the exile was decreed from years before, and there was no way out of it. But no decree decided that this exile had to be in Mitzrayim. It could have been anywhere else. Mitzrayim was a iron furnace, with heavy heat and difficult experiences. The expression of pain was about the terrible galus that the Bnei Yisroel underwent in Mitzrayim.
Another reason for the pain is because of the news of the hunger that was to overcome the world. Hashem does not like when His world must suffer, even as He causes it.
Another reason for the suffering alluded to with this word is the fact that the Tzadik had to wait two years to be redeemed from the dungeons. Chazal tell us that Yosef was supposed to be redeemed two years earlier, but he was punished for placing his trust in the Royal Butler instead of in Hashem. Yosef had an additional two years attached to his sentence, and that is a source of pain. The Medrash tells us that the word מִקֵּץ is alluding to the words in Tehillim קץ שם לחושך – he placed an end to the darkness. The word קץ alludes to the Yetzer Hara, as he is the end of all flesh. Due to the Yetzer Hara, Hashem waited two years, and then redeemed Yosef from jail.
This also explains when the two years started. Now that we know that the possuk is describing the pain that Yosef underwent with his two-year wait, we know that the two years began with the dreams of the Royal butler and baker.
The Ohr Hachaim then proceeds to explain why it took two years. Yosef asked the Royal butler to remember him, using a double expression זכרתני והזכרתני. Due to this, he was ignored over two זכירות of Rosh Hashana. The Rosh Hashana after he said this, when he had the opportunity to be remembered for salvation, he was ignored. As was the next one. After losing two such opportunities, he merited to be saved.