How to close this stream ?

async Task<StorageFile> CropImage(StorageFile storageFile, ImageCropper imageCropper)
{
var copiedStorageFile = await storageFile.CopyAsync(ApplicationData.Current.TemporaryFolder, storageFile.Name, NameCollisionOption.ReplaceExisting);
using (var fileStream = await copiedStorageFile.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None))
{
await imageCropper.SaveAsync(fileStream, BitmapFileFormat.Png);
}
return copiedStorageFile;
}
async Task<StorageFile> CropImage(StorageFile storageFile, ImageCropper imageCropper)
{
var copiedStorageFile = await storageFile.CopyAsync(ApplicationData.Current.TemporaryFolder, storageFile.Name, NameCollisionOption.ReplaceExisting);
using (var fileStream = await copiedStorageFile.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None))
{
await imageCropper.SaveAsync(fileStream, BitmapFileFormat.Png);
}
return copiedStorageFile;
}
5 Replies
333fred
333fred3y ago
Whch stream? The fileStream? If so, the using will take care of that for you
TotechsStrypper
TotechsStrypperOP3y ago
When I reuse the crop image it said being used by other process
The process cannot access the file because it is being used by another process. (Exception from HRESULT: 0x80070020)
The process cannot access the file because it is being used by another process. (Exception from HRESULT: 0x80070020)
becquerel
becquerel3y ago
are you sure you're not getting that error from trying to open the file?
TotechsStrypper
TotechsStrypperOP3y ago
ah no it's from the CopyAsync when trying to crop the same image
Susko3
Susko33y ago
Try changing the temp file name, mange the old name with a Guid.NewGuid

Did you find this page helpful?