位图图像setsource:类型' System.ArgumenException'的第一次机会异常。发生在mscorlib.ni.dll中

时间:2014-08-16 15:24:29

标签: c# windows-runtime portable-class-library bitmapimage

在Windows Universal应用程序中,我正在尝试将可移植库中的图像加载到位图图像中,然后将其放入Image控件中。我遇到的问题是我似乎陷入了bitmapImage.SetSourceAsync(stream)。我找到another user with the problem,但尚未找到解决方案。 我是否需要在png文件上进行某种转换?

编辑我无法真正称之为解决方案,但如果我将图像移出库并进入实际项目,它就可以正常工作。我现在只想和它一起生活。

这是我的代码:

    private async Task<BitmapImage> LoadImageFromLibrary(string original)
    {
        var asm = Assembly.Load(new AssemblyName(LibraryAsmName)); //@"MyProjectLibrary
        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>
/// </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();
        }
    }
}

0 个答案:

没有答案