0

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);
  • What is `encoder`? Looks like you want to make a vid or gif with frames? If its the latter I've written the "Make an Animate Gif" code here https://stackoverflow.com/a/13486374/495455 – Jeremy Thompson Feb 21 '22 at 05:21
  • Also your `ToImage` function looks odd - arrh its a void not a function and I'm still guessing what `encoder` is, but see the last two functions here showing what a typical ByteArray to Image converter function looks like: https://stackoverflow.com/a/26729513/495455 – Jeremy Thompson Feb 21 '22 at 05:24
  • Jeremy Thompson, I was using var encoder = new PngBitmapEncoder(); to set my pixel format – MiltaruyOne Feb 21 '22 at 05:50
  • `var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);`. So you preserve the image. `decoder.Frames[0]` is your BitmapSource. -- You can set the `StreamSource` of a BitmapImage to a new MemoryStream (i.e., don't overwrite the current Stream), if you actually need a BitmapImage. – Jimi Feb 21 '22 at 08:10
  • Jimi, please see my update. Is that what you meant ? I got a runtime error – MiltaruyOne Feb 21 '22 at 15:17
  • Try to set `stream.Position = 0;` as the first line in the `using` block. It that still generates the same error, then the byte array is missing something or the image is not compatible/compromised. In this case, specify where the array is coming from and what kind of bitmap it represents. -- Note that, to get a BitmapImage (again, if that's actually what you need), you can use `image.SetSource(stream);` – Jimi Feb 21 '22 at 17:16

0 Answers0