System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream throws a Null Reference Exception trying to close a null BitmapStream when called from XpfPainter.CreatePlatformImageSourceCore.
C#static ImageSource CreatePlatformImageSourceCore(OfficeImage img) {
BitmapImage imageSource = new BitmapImage();
imageSource.BeginInit();
byte[] bytes = img.GetImageBytesSafe(img.RawFormat);
imageSource.StreamSource = new System.IO.MemoryStream(bytes);
if (img.SizeInPixels.Width > maxImageSize || img.SizeInPixels.Height > maxImageSize) {
double aspectRatio = img.SizeInPixels.Width / img.SizeInPixels.Height;
int width, height;
if (img.SizeInPixels.Width > maxImageSize) {
width = maxImageSize;
height = (int)Math.Round(width / aspectRatio);
}
else {
height = maxImageSize;
width = (int)Math.Round(height * aspectRatio);
}
imageSource.DecodePixelWidth = width;
imageSource.DecodePixelHeight = height;
}
//Null Reference Exception Here
imageSource.EndInit();
imageSource.Freeze();
return imageSource;
}