I'm trying to convert an uploaded file into an Image object, so I can resize it before saving. However, the following line causes an error:
System.Drawing.Image profileImg = System.Drawing.Image.FromStream(ProfileImg.FileContent);
The process cannot access the file because it is being used by another process.
How do I convert the uploaded file into an Image object?
I was able to get it to work using this slightly modified code:
Stream imgStream = ProfileImg.FileContent;
System.Drawing.Image profileImg = System.Drawing.Image.FromStream(imgStream);
Not entirely sure what the difference is, as it's basically the same code. Any idea why this works and the other doesn't?
You're right. I was closing the stream on the second example, but not on the first.
Here is the fix for the first example:
ProfileImg.FileContent.Close(); // this closes the stream