Reputation: 81
Am using BitmapSource class for reading an image from my temp folder and then reading the metadata using BitmapMetadata class.
BitmapSource img = BitmapFrame.Create(new Uri(filepath));
BitmapMetadata meta = (BitmapMetadata)img.Metadata;
DateTime datetaken = DateTime.Parse(meta.DateTaken);
System.IO.File.Delete(filepath);
While i was trying to delete the image am getting an exception saying "The process cannot access the file 'filepath/filename' because it is being used by another process.".I thought of disposing the bitmapsource before deleting the image. While i was searching for the solution i got info like "You do not have to Dispose() a BitmapSource. Unlike some other "image" classes in the Framework, it does not wrap any native resources.
Just let it go out of scope, and the garbage collector will free its memory." in the following link Proper way to dispose a BitmapSource .I just want to delete the file that exists in the physical folder. Is there any proper way for the deletion of physical path. Thanks in advance.
Upvotes: 4
Views: 2374
Reputation: 2638
You could do as the top suggested answer here and copy the file to a stream first and initialize the bitmap source from the stream e.g.
MemoryStream memoryStream = new MemoryStream();
byte[] fileBytes = File.ReadAllBytes(filepath);
memoryStream.Write(fileBytes, 0, fileBytes.Length);
memoryStream.Position = 0;
BitmapSource img = BitmapFrame.Create(memoryStream);
BitmapMetadata meta = (BitmapMetadata)img.Metadata;
DateTime datetaken = DateTime.Parse(meta.DateTaken);
System.IO.File.Delete(filepath);
I've tried this and it works for me
Upvotes: 1