5

How Do I convert BitmapSource to MemoryStream. Though I tried some code:

private Stream StreamFromBitmapSource(BitmapSource writeBmp)
{
    Stream bmp;
    using (bmp = new MemoryStream())
    {                    
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(writeBmp));
        enc.Save(bmp);                                     
    }

   return bmp;
}

It doesn't give any error but after putting debugging point it is showing some exceptions which are listed below.

Capacity: 'printStream.Capacity' threw an exception of type 'System.ObjectDisposedException' Length: 'printStream.Length' threw an exception of type 'System.ObjectDisposedException' Position: 'printStream.Position' threw an exception of type 'System.ObjectDisposedException'

BWA
  • 5,672
  • 7
  • 34
  • 45
Aarti Dhiman
  • 75
  • 1
  • 9
  • Thanks! Clemens, I gave my votes to the answers posted. :), and I want my color frames to get save on memory buffer to use easily with `face recognition API`.So, I'm returning my result to a `Stream` which my `face recognition API` will utilize. – Aarti Dhiman Feb 16 '17 at 15:39

2 Answers2

8

using (bmp = new MemoryStream()) causes bmp object is destroyed on end using block. And You return bmp variable which is destroyed.

Remove using:

private Stream StreamFromBitmapSource(BitmapSource writeBmp)
{
    Stream bmp = new MemoryStream();

    BitmapEncoder enc = new BmpBitmapEncoder();
    enc.Frames.Add(BitmapFrame.Create(writeBmp));
    enc.Save(bmp);                                             

   return bmp;
}
BWA
  • 5,672
  • 7
  • 34
  • 45
  • `ReadTimeout = 'imgStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'` `WriteTimeout = 'imgStream.WriteTimeout' threw an exception of type 'System.InvalidOperationException'` Thanks for helping me out. But now I'm facing trouble with these two exceptions – Aarti Dhiman Feb 16 '17 at 15:32
0

The problem here is that you are creating bmp inside an using, that's why it has been disposed before you return it (it is disposed once you leave the using) and that explains the exceptions you receive.

private Stream StreamFromBitmapSource(BitmapSource writeBmp)
{
    Stream bmp= new MemoryStream();
    using (enc = new BmpBitmapEncoder())
    {                    
        enc.Frames.Add(BitmapFrame.Create(writeBmp));
        enc.Save(bmp);                                     
    }

   return bmp;
}
moondaisy
  • 4,303
  • 6
  • 41
  • 70
  • `ReadTimeout = 'imgStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'` `WriteTimeout = 'imgStream.WriteTimeout' threw an exception of type 'System.InvalidOperationException'` Thanks for helping me out. But now I'm facing trouble with these two exceptions – Aarti Dhiman Feb 16 '17 at 15:34
  • Could help http://stackoverflow.com/questions/28172110/readtimeout-exception-with-memorystream – moondaisy Feb 16 '17 at 16:03
  • No, It isn't helping and I'm using `BitmapSource` instead of `Bitmap` – Aarti Dhiman Feb 16 '17 at 16:37