You'll probably have to save the image as a JPG and check the file size, if it's too big save it again with a higher compression, check the size and so on. Code for changing compression ratio can be found here
How to: Set JPEG Compression Level | Microsoft Docs[
^]
pseudocode would be something like
quality = 100
save image at quality
do while image size > 2MB and quality > 10
quality = quality - 10
save image at quality
end do
To preempt your next question, no I won't write it for you, there are loads of examples on how to upload images and save them, and I've shown you the code for how to save at a given quality, so google those samples, understand them, then put them together.