I have a MemoryStream within a using statement that I pass as a parameter to another method, however the 2nd time the method is called, the memory stream is closed. Below is the error and the code. Any idea what could cause this?
Cannot access a closed stream.
public async Task UploadObject(IFormFile file, string prefix)
{
using (var memoryStream = new MemoryStream())
{
file.CopyTo(memoryStream);
await UploadImageAsync(memoryStream, false));
await UploadImageAsync(memoryStream, true));
}
}
public async Task UploadImageAsync(MemoryStream stream, bool process)
{
if (process)
{
using (Image orginalImage = Image.FromStream(stream)) **<<<< ERROR HERE**
{
using (Bitmap bitmapResized = new Bitmap(orginalImage, newWidth, newHeight))
{
using (MemoryStream streamResized = new MemoryStream())
{
bitmapResized.Save(streamResized, orginalImage.RawFormat);
await UploadAsync(streamResized);
}
}
}
}
else
{
await UploadAsync(stream);
}
async Task UploadAsync(MemoryStream stream)
{
// stream used here to create uploadrequest
await fileTransferUtility.UploadAsync(uploadRequest);
}
}