- Thread Author
- #1
Hi,
I am loading images manually in my code as follows:
When I call inStream.Dispose sometimes the images are not displayed.
It seems that the file is disposed before the bitmap image completed to copy it.
When should I dispose my fine in order to avoid memory leaks?
private async Task LoadBitmap(string path, BitmapImage bi)
{
bi.DecodePixelWidth = DecodeWidth;
//bi.CacheOption = BitmapCacheOption.OnLoad;
//bi.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
StorageFile file = null;
try
{
var uri = new Uri(path);
file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
}
catch (Exception)
{
}
if (file == null)
{
file = await ApplicationData.Current.RoamingFolder.GetFileAsync(path);
}
if (file != null)
{
IRandomAccessStream inStream = await file.OpenReadAsync();
try
{
bi.SetSource(inStream);
}
finally
{
//inStream.Dispose();
}
}
}
I am loading images manually in my code as follows:
When I call inStream.Dispose sometimes the images are not displayed.
It seems that the file is disposed before the bitmap image completed to copy it.
When should I dispose my fine in order to avoid memory leaks?
private async Task LoadBitmap(string path, BitmapImage bi)
{
bi.DecodePixelWidth = DecodeWidth;
//bi.CacheOption = BitmapCacheOption.OnLoad;
//bi.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
StorageFile file = null;
try
{
var uri = new Uri(path);
file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
}
catch (Exception)
{
}
if (file == null)
{
file = await ApplicationData.Current.RoamingFolder.GetFileAsync(path);
}
if (file != null)
{
IRandomAccessStream inStream = await file.OpenReadAsync();
try
{
bi.SetSource(inStream);
}
finally
{
//inStream.Dispose();
}
}
}