简体   繁体   中英

Load Image from Stream

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).

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM