Images are displayed randomly

Discussion in 'Windows 8 Help and Support' started by Hagit, Jul 1, 2012.

  1. Hagit

    Hagit New Member

    Joined:
    Jul 1, 2012
    Messages:
    1
    Likes Received:
    0
    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();
    }
    }

    }
     

Share This Page

Loading...