var filePath= @"c:\user\image.tif";
TiffBitmapDecoder TifDec = new TiffBitmapDecoder(new Uri(path), BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnDemand);
File.Delete(pathFile);
is throw exception "System.IO.IOException: 'The process cannot access the file 'c:\user\image.tif' because it is being used by another process."
this code not working
var filePath = @"c:\user\image.tif";
TiffBitmapDecoder TifDec = new TiffBitmapDecoder(new Uri(filePath), BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnDemand);
TifDec.Dispatcher.InvokeShutdown();
TifDec.Dispatcher.DisableProcessing();
while (!TifDec.Dispatcher.HasShutdownFinished)
Thread.Sleep(10);
File.Delete(filePath);
Sadly, it seems that there is no way to do this cleanly.
The stream is closed in the finalizer, but there's no way to force that code to run. It's also closed in the CloseStream
method, but that's internal.
So avoid using the Uri
overload. Instead, open the file yourself, and use the Stream
overload of TiffBitmapDecoder
. You can then dispose the stream at will, and delete the file as soon as you release the stream. Of course, you shouldn't use the TiffBitmapDecoder
after you release the stream.
If you want to delete the file even sooner, just create a memory stream, fill it with data from File.ReadAllBytes
, and use that for the decoder.