C# BitmapImage OutOfMemoryException仅适用于Windows 8.1

C# BitmapImage OutOfMemoryException仅适用于Windows 8.1,c#,out-of-memory,windows-8.1,bitmapimage,C#,Out Of Memory,Windows 8.1,Bitmapimage,我最近安装了Windows8.1进行尝试,我最喜欢的项目正在崩溃(相同的二进制文件在一台Win7和两台Win8机器上运行良好) 正在BitmapImage.EndInit中引发OutOfMemoryException: public static BitmapImage GetResourceImage(string resourcePath, int decodePixelWidth = 0, int decodePixelHeight = 0) { var i

我最近安装了Windows8.1进行尝试,我最喜欢的项目正在崩溃(相同的二进制文件在一台Win7和两台Win8机器上运行良好)

正在BitmapImage.EndInit中引发OutOfMemoryException:

    public static BitmapImage GetResourceImage(string resourcePath, int decodePixelWidth = 0, int decodePixelHeight = 0)
    {
        var image = new BitmapImage();

        var moduleName = Assembly.GetExecutingAssembly().GetName().Name;
        var resourceLocation = string.Format("pack://application:,,,/{0};component/Resources/{1}", moduleName, resourcePath);

        image.BeginInit();
        image.UriSource = new Uri(resourceLocation);
        image.DecodePixelWidth = decodePixelWidth;
        image.DecodePixelHeight = decodePixelHeight;
        image.EndInit();
        image.Freeze();

        return image;
    }
  • 崩溃时进程的任务管理器读数:27MB内存、302个句柄、12个线程、36个用户对象、34个GDI对象、5.3 GB RAM可用
  • 平台目标为x86(由于使用本机DLL,因此必需)
  • 对于不同的(随机)图像(参考资料中约有250个bmp文件),多次调用该方法
  • 解码像素宽度/高度是随机的,但在有效范围内
  • 异常发生在不同的资源路径和大小上
  • 仅当DecodePixelWidth或DecodePixelHeight不为0时,才会发生异常
  • 若我注释掉DecodePixelWidth和DecodePixelHeight设置器,并让它加载到原始大小,则不会发生异常
正如我从谷歌上了解到的,它与GDI内部结构有关,但我找不到修复或解决方法。有什么想法吗(除了使用其他机制解码和/或调整图像大小-我只需要原始像素数据)

完整的项目源代码可在此处找到(链接到相关文件):

更新:
我曾尝试使用TransformedBitmap调整大小,但失败了,出现了相同的异常。

我创建了一个单独的项目并隔离了该问题。 在GDI中,256色位图看起来像是一个非常奇怪的bug(我所有的图片都是用QBasic编写的在校游戏中的256色位图)

  • png和24位位图没有问题
  • 加载24位位图后,调整256色位图大小的问题似乎消失了
因此,通过将所有内容转换为PNG,我的问题得到了解决

错误已发布到Microsoft Connect:

下面是代码

internal class Program
{
    public static BitmapSource GetResourceImage(string resourcePath, int decodePixelWidth = 0,
        int decodePixelHeight = 0)
    {
        var image = new BitmapImage();

        var moduleName = Assembly.GetExecutingAssembly().GetName().Name;
        var resourceLocation = string.Format("pack://application:,,,/{0};component/Resources/{1}", moduleName, resourcePath);

        image.BeginInit();
        image.UriSource = new Uri(resourceLocation);
        image.DecodePixelWidth = decodePixelWidth;
        image.DecodePixelHeight = decodePixelHeight;
        image.EndInit();
        image.Freeze();
        return image;
    }


    private static void Main()
    {
        new Application();  // register pack uri scheme

        //GetResourceImage("Z40100.png");
        //GetResourceImage("Z40100.bmp");
        //GetResourceImage("Z40100_256color.bmp");
        //GetResourceImage("Z40100_24bit.bmp");

        // Uncomment the following line to fix the crash (or any of the two lines above)

        //GetResourceImage("Z40100_24bit.bmp", 50, 50);
        //GetResourceImage("Z40100_256color.bmp", 50, 50);
        GetResourceImage("Z40100.bmp", 50, 50);
        // GetResourceImage("Z40100.png", 50, 50);
    }
}

它在任何特定的文件类型上都会失败吗?@JoachimIsaksson,不,它不会。大约有250个类似的100x100位图图像。每次它在另一个上失败。@JoachimIsaksson,可能是我误解了你,而你实际上是对的-请参阅我发布的答案。在Windows 2012上也是如此