C# BitmapImage OutOfMemoryException仅适用于Windows 8.1
我最近安装了Windows8.1进行尝试,我最喜欢的项目正在崩溃(相同的二进制文件在一台Win7和两台Win8机器上运行良好) 正在BitmapImage.EndInit中引发OutOfMemoryException: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
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设置器,并让它加载到原始大小,则不会发生异常
我曾尝试使用TransformedBitmap调整大小,但失败了,出现了相同的异常。我创建了一个单独的项目并隔离了该问题。 在GDI中,256色位图看起来像是一个非常奇怪的bug(我所有的图片都是用QBasic编写的在校游戏中的256色位图)
- png和24位位图没有问题
- 加载24位位图后,调整256色位图大小的问题似乎消失了
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上也是如此