加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 仅在Windows 8.1中的BitmapImage OutOfMemoryException

发布时间:2020-12-15 05:41:40 所属栏目:百科 来源:网络整理
导读:我最近安装了 Windows 8.1来尝试,我的宠物项目正在崩溃(同样的二进制文件在一台Win7和两台Win8机器上工作正常). 在BitmapImage.EndInit中抛出OutOfMemoryException: public static BitmapImage GetResourceImage(string resourcePath,int decodePixelWidth
我最近安装了 Windows 8.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 / Height是随机的,但在有效范围内
>异常发生在不同的资源路径和大小上
>仅当DecodePixelWidth或DecodePixelHeight不为0时才会发生异常
>如果我注释掉DecodePixelWidth和DecodePixelHeight setter并将其加载到原始大小,则不会发生异常

据我通过谷歌搜索了解,它与GDI内部有关,但我找不到修复或解决方法.任何想法(除了使用一些其他机制来解码和/或调整图像大小 – 我只需要原始像素数据)?

完整的项目源代码可以在这里找到(链接到相关文件):https://code.google.com/p/lander-net/source/browse/trunk/csharp/LanderNet/Util/BitmapUtils.cs

更新:
我试图使用TransformedBitmap进行大小调整,它失败并出现相同的异常.

解决方法

我创建了一个单独的项目并将问题隔离开来.
在GDI中看起来像一个非常奇怪的错误,有256个颜色的位图(我的所有图像都是从我在QBasic写的学校时代游戏中获得的256色位图).

> png和24位位图没有问题
>加载24位位图后,调整256色位图的问题似乎消失了

所以我的问题是通过将所有内容转换为PNG来解决的.

错误发布到Microsoft Connect:https://connect.microsoft.com/VisualStudio/feedback/details/812641/bitmapsource-fails-with-outofmemoryexception-on-8-bit-bitmaps

下面是代码.

internal class Program
{
    public static BitmapSource GetResourceImage(string resourcePath,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);
        GetResourceImage("Z40100.bmp",50);
        // GetResourceImage("Z40100.png",50);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读