locked
bitmapImage setsourceasync hangs or never completes

    Question

  • in a Windows Universal app, I am trying to load an image from a portable library into a bitmap image that I will then put into an Image control. The problem I'm having is that I seem to get stuck on bitmapImage.SetSourceAsync(stream). I've found another user with the problem, but a solution hasn't been found. Do I need to do some sort of conversion on the png file?

    Here is my code:

    private async Task<BitmapImage> LoadImageFromLibrary(string original)
        {
            var asm = Assembly.Load(new AssemblyName(LibraryAsmName));
            string imageName = original + Extension; // e.g. Hir.png
            var bm = new BitmapImage();
    
            using (var stream = ResourceLoader.GetEmbeddedResourceStream(asm, imageName)
                .AsRandomAccessStream()) // shows a size equal to the size of the .png file
            {
                //await Task.Run(() => stream.Size); // possible online solution... Nope, didn't work.
                await bm.SetSourceAsync(stream); // gets stuck here. Output window shows "A first chance exception of type 'System.ArgumenException' occurred in mscorlib.ni.dll"
            }
            return bm;
        }
    /// <summary>
        /// Utility class that can be used to find and load embedded resources into memory.
        /// </summary>
        /// <remarks>
        /// https://github.com/xamarin/mobile-samples/blob/master/EmbeddedResources/SharedLib/ResourceLoader.cs
        /// </remarks>
        public static class ResourceLoader
        {
            /// <summary>
            /// Attempts to find and return the given resource from within the specified assembly.
            /// </summary>
            /// <returns>The embedded resource stream.</returns>
            /// <param name="assembly">Assembly.</param>
            /// <param name="resourceFileName">Resource file name.</param>
            public static Stream GetEmbeddedResourceStream(Assembly assembly, string resourceFileName)
            {
                var resourceNames = assembly.GetManifestResourceNames();
    
                var resourcePaths = resourceNames
                    .Where(x => x.EndsWith(resourceFileName, StringComparison.CurrentCultureIgnoreCase))
                    .ToArray();
    
                if (!resourcePaths.Any())
                {
                    throw new Exception(string.Format("Resource ending with {0} not found.", resourceFileName));
                }
    
                if (resourcePaths.Count() > 1)
                {
                    throw new Exception(string.Format("Multiple resources ending with {0} found: {1}{2}", resourceFileName, Environment.NewLine, string.Join(Environment.NewLine, resourcePaths)));
                }
    
                return assembly.GetManifestResourceStream(resourcePaths.Single());
            }
    
            /// <summary>
            /// Attempts to find and return the given resource from within the specified assembly.
            /// </summary>
            /// <returns>The embedded resource as a byte array.</returns>
            /// <param name="assembly">Assembly.</param>
            /// <param name="resourceFileName">Resource file name.</param>
            public static byte[] GetEmbeddedResourceBytes(Assembly assembly, string resourceFileName)
            {
                var stream = GetEmbeddedResourceStream(assembly, resourceFileName);
    
                using (var memoryStream = new MemoryStream())
                {
                    stream.CopyTo(memoryStream);
                    return memoryStream.ToArray();
                }
            }
    
            /// <summary>
            /// Attempts to find and return the given resource from within the specified assembly.
            /// </summary>
            /// <returns>The embedded resource as a string.</returns>
            /// <param name="assembly">Assembly.</param>
            /// <param name="resourceFileName">Resource file name.</param>
            public static string GetEmbeddedResourceString(Assembly assembly, string resourceFileName)
            {
                var stream = GetEmbeddedResourceStream(assembly, resourceFileName);
    
                using (var streamReader = new StreamReader(stream))
                {
                    return streamReader.ReadToEnd();
                }
            }
        }



    Michael DiLeo


    • Edited by mcd023 Saturday, August 16, 2014 4:54 PM added resource loader class
    Saturday, August 16, 2014 3:41 PM

Answers

  • Hi Michael,

      >> await bm.SetSourceAsync(stream); // gets stuck here. Output window shows "A first chance exception of type 'System.ArgumenException' occurred in mscorlib.ni.dll"

    Based on the error message, I'd like to suggest you to set a breakpoint at:
    var resourceNames = assembly.GetManifestResourceNames();
    to check whether the resourceNames is empty or not. If the result is empty, per my experience, the issue maybe related to you have not set the image (// e.g. Hir.png) ‘Build Action’ as ‘Embedded Resource’, thus assembly.GetManifestResourceNames could not get the resource as expected, then the stream that assigned to bm.SetSourceAsync(stream) would causes this error. For setting a resource ‘Build Action’ as ‘Embedded Resource’, you can e.g. left click on the image -> press keyboard F4 to open file properties window. In addition, you may want to refer to this tip: http://www.stuartgillibrand.co.uk/2011/09/getmanifestresourcestream-returns-null.html if your images live under a folder.

    Also, it would be better if you can check whether you could see any expected exceptions that you have mentioned at:

    if (!resourcePaths.Any())
               
    {
                   
    throw new Exception(string.Format("Resource ending with {0} not found.", resourceFileName));
               
    }

               
    if (resourcePaths.Count() > 1)
               
    {
                   
    throw new Exception(string.Format("Multiple resources ending with {0} found: {1}{2}", resourceFileName, Environment.NewLine, string.Join(Environment.NewLine, resourcePaths)));
               
    }
     

    Should you have any concern, please feel free to let us know.
    Thank you.

    Best Regards,

    Ming Xu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, August 21, 2014 6:03 AM
    Moderator

All replies

  • Hi Michael,

    Please post a project to help reproduce. I am trying to involve someone familiar with this issue to come into it. I will update this thread when I get result as soon as possible.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 18, 2014 10:18 AM
    Moderator
  • Thank you for the assistance. How may I PM you?

    Michael DiLeo

    Monday, August 18, 2014 10:44 PM
  • Use OneDrive to post your test project. https://onedrive.live.com/.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 19, 2014 1:41 AM
    Moderator
  • I've created a portion of my project. Please look at the ImageHelper class in the universal project's Helpers folder.
    http://1drv.ms/1pHUfo5

    Michael DiLeo

    Tuesday, August 19, 2014 9:10 PM
  • Hi Michael,

      >> await bm.SetSourceAsync(stream); // gets stuck here. Output window shows "A first chance exception of type 'System.ArgumenException' occurred in mscorlib.ni.dll"

    Based on the error message, I'd like to suggest you to set a breakpoint at:
    var resourceNames = assembly.GetManifestResourceNames();
    to check whether the resourceNames is empty or not. If the result is empty, per my experience, the issue maybe related to you have not set the image (// e.g. Hir.png) ‘Build Action’ as ‘Embedded Resource’, thus assembly.GetManifestResourceNames could not get the resource as expected, then the stream that assigned to bm.SetSourceAsync(stream) would causes this error. For setting a resource ‘Build Action’ as ‘Embedded Resource’, you can e.g. left click on the image -> press keyboard F4 to open file properties window. In addition, you may want to refer to this tip: http://www.stuartgillibrand.co.uk/2011/09/getmanifestresourcestream-returns-null.html if your images live under a folder.

    Also, it would be better if you can check whether you could see any expected exceptions that you have mentioned at:

    if (!resourcePaths.Any())
               
    {
                   
    throw new Exception(string.Format("Resource ending with {0} not found.", resourceFileName));
               
    }

               
    if (resourcePaths.Count() > 1)
               
    {
                   
    throw new Exception(string.Format("Multiple resources ending with {0} found: {1}{2}", resourceFileName, Environment.NewLine, string.Join(Environment.NewLine, resourcePaths)));
               
    }
     

    Should you have any concern, please feel free to let us know.
    Thank you.

    Best Regards,

    Ming Xu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, August 21, 2014 6:03 AM
    Moderator