I've created a separate project and isolated the issue. Looks like a very strange bug in GDI with 256 color bitmaps (all my images are 256 color bitmaps taken from my school days game written in QBasic).
- there are no problems with png and 24 bit bitmaps
- problems with resizing 256 color bitmaps seem to go away after loading a 24 bit bitmap
So my issue is fixed by converting everything to PNG.
Bug is posted to Microsoft Connect: https://connect.microsoft.com/VisualStudio/feedback/details/812641/bitmapsource-fails-with-outofmemoryexception-on-8-bit-bitmaps
Below is the code.
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);
}
}