Since you are writing into the memory stream, and get the error there implies that the error is actually a fair way away from this point. Have a look at where you load the Image - if the original stream you loaded the image from is not still open, then you will get this error - it's in the documentation for
Image.FromStream[
^] tyhat the stream must remain open for teh life of the Image.
Similarly, if you use Image.FromFile, the source file will be locked for the life of the image.
One way to get round this is to load the Image, draw it into a new Bitmap object and then dispose the original and use the copy.