Home > database >  MAUI: Not able to convert camera captured image into byte[] array to send it to mongo db
MAUI: Not able to convert camera captured image into byte[] array to send it to mongo db

Time:11-03

My aim is to take a picture with my phone and store it in mongodb. For that, I need to convert the image to byte[] array and will store in mongo db as binary data. My code is in .NET MAUI. I am able to take the picture with camera but not able to convert to byte[]. Could anybody plz provide me some guidance here. The code is failing in the highlighted line in the attached picture, and the error is shown below. The error says "because the jpg file is being used by another process."

I tried to sleep the thread for some time but of no use.Code snippet and the error

CodePudding user response:

Your using statement isn't disposing of localFileStream until the end of the block. That's why the file is still being used (admittedly the "by another process" is misleading) when you try to read from it. You almost certainly want to close it as soon as you've finished writing the data - so use an old-school using statement:

using (FileStream localFileStream = File.OpenWrite(localFilePath))
{
    await sourceScrem.CopyToAsync(localFileStream);
}
  • Related