I have the following code:
BitmapSource bitmap = _bitmapSource;
if (_bitmapSource.Format != PixelFormats.Bgra32)
bitmap = new FormatConvertedBitmap(_bitmapSource, PixelFormats.Bgra32, null, 0);
int bytesPerPixel = (bitmap.Format.BitsPerPixel + 7) / 8;
int pixelWidth = bitmap.PixelWidth;
int pixelHeight = bitmap.PixelHeight;
int stride = bytesPerPixel * pixelWidth;
int pixelCount = pixelWidth * pixelHeight;
var pixelBytes = new byte[pixelCount * bytesPerPixel];
bitmap.CopyPixels(pixelBytes, stride, 0);
...
}
A NUnit test exercises this code which throws when it reaches bitmap.CopyPixels:
System.IO.FileFormatException : The image decoder cannot decode the image. The image might be corrupted.
----> System.Runtime.InteropServices.COMException : Exception from HRESULT: 0x88982F60
at System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(Int32Rect sourceRect, IntPtr buffer, Int32 bufferSize, Int32 stride)
at System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(Int32Rect sourceRect, Array pixels, Int32 stride, Int32 offset)
at System.Windows.Media.Imaging.BitmapSource.CopyPixels(Int32Rect sourceRect, Array pixels, Int32 stride, Int32 offset)
at System.Windows.Media.Imaging.BitmapSource.CopyPixels(Array pixels, Int32 stride, Int32 offset)
however the image is not corrupted (other tests use the same file without issue) and strangely if I set a breakpoint at bitmap.CopyPixels and break in the debugger then continue, the exception is not thrown.
Can anyone shed any light on what might be causing the error for me?
I have solved this, it was quite simple.
_bitmapSourcewas created earlier using a FileStream like so:In the docs for
BitmapFrame.Createit saysSo I needed to do this:
My NUnit test now passes.