5

Is there any way of converting Stream to Image?

I tried Bitmap, but it states that I don't have System.Drawing... so I tried this:

var bitMap = new BitmapImage();
bitMap.SetSourceAsync(stream);
image.Source = bitMap;

EDIT: I am trying to build UWP app + using VS 2015.

2 - It just states that System.Drawing does not exist in the namespace.

EDIT2:

Ok, I might have explained it wrong. The idea is: I have an Image, and I want to change its source to something different and then for it to reload, so I can see the image. The image is effectively a "Stream", so I assume I need to convert it to Bitmap and then load somehow.

EDIT3:

Ok, so I think it will be easier to describe and then use the code above:

There is a picture box and I am using:

var stream = new InMemoryRandomAccessStream();
await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);

Now I would like this Captured Photo to be displayed as an Image. ( Image "box" is created at the start, so the idea is to change source).

Johhny Bravo
  • 199
  • 3
  • 15

2 Answers2

6

Right, so I managed to fix it:

var bitMap = new BitmapImage();
stream.seek(0);                          // LINE ADDED
bitMap.SetSourceAsync(stream);
image.Source = bitMap;

I turned out that the error that was being produced was : "The component cannot be found.", so I managed to fix it by using this trick.

Johhny Bravo
  • 199
  • 3
  • 15
  • You are a gentleman and a scholar. It looked everywhere for an explanation, but everyone else is loading into a stream from a file starting at position zero. There was some other code that said stream.position = 0 but this is write only in UWP. – sonofsmog Apr 08 '17 at 04:03
  • I could not get this approach to work for me, but this worked a charm: https://social.msdn.microsoft.com/Forums/en-US/6590eb57-98cf-4b9f-b2d8-6e7aae9c00a2/cant-convert-byte-to-bitmapimage-component-cannot-be-found-error?forum=winappswithcsharp – Adrian K Jul 31 '18 at 00:45
2

I am not sure if this is what you are looking for but if you would like to use stream with BitMapImage you should use:

var image = new BitmapImage();
await image.SetSourceAsync(stream);

For instance when you have your photo stored as a byte[] array you can use the stream to convert it to image:

using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
 {
     DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0)) 
     writer.WriteBytes(<<here your byte[] array>>);
     await writer.StoreAsync();

     var image = new BitmapImage();
     await image.SetSourceAsync(stream);
 }

Is that what you need?

Daniel Krzyczkowski
  • 2,732
  • 2
  • 20
  • 30
  • Right, do you have to keep it stored as a byte[] can't you just use the stream? Secondly, the issue is that the Image doesn't load the new source, is there a way of 'reloading' it? – Johhny Bravo Jun 24 '16 at 15:21
  • I have this as a byte[] array because this is one of my object's property. It is retrieved from the web service like that. With reference to second issue - what do you mean not loading new source? – Daniel Krzyczkowski Jun 24 '16 at 15:24
  • Ok, so if you look at my code. I am doing exactly the same thing as you are... by accident I forgot to add "await" , but that was 'Undo' fault... However, the idea is that at the end, I have 'var image' displayed in a separate 'image' box, and that what seems not to be hapenning. – Johhny Bravo Jun 24 '16 at 15:27
  • I am not sure if I understand correctly. But once you get your stream you should set it for the image - this should display it. – Daniel Krzyczkowski Jun 24 '16 at 15:32
  • Actually, I think I know what caused it: I am getting this ( I didn't see it before) : An exception of type 'System.Exception' occurred in mscorlib.ni.dll but was not handled in user code It happens, when I launch " await image.SetSourceAsync(stream);" – Johhny Bravo Jun 24 '16 at 15:37
  • In this case I am not able to reproduce it unfortunately. – Daniel Krzyczkowski Jun 24 '16 at 15:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115540/discussion-between-johnny-bravo-and-daniel-krzyczkowski). – Johhny Bravo Jun 24 '16 at 15:47