I keep getting this error: Operation is not valid due to the current state of the object here is my code
public BitmapImage ToImage(byte[] array)
{
using (var stream = new MemoryStream(array))
{
var encoder = new PngBitmapEncoder();
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad; // here
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
// image.StreamSource = stream;
image.EndInit();
return image;
}
}
any ideas?
Update: public BitmapImage ToImage(byte[] array) {
using (var stream = new MemoryStream(array))
{
var encoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad; // here
// image.StreamSource = stream;
image.EndInit();
return image;
}
}
I got this error : No imaging component suitable to complete this operation was found.' Inner Exception COMException: The component cannot be found. (Exception from HRESULT: 0x88982F50)
on
var encoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);