Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Image from Stream

Tags:

c#

uwp

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

like image 309
Johhny Bravo Avatar asked Jun 24 '16 14:06

Johhny Bravo


People also ask

How do I read images from a stream?

If you want to read from a stream you will need to tell Magick.NET the format of the file: var settings = new MagickReadSettings { Format = MagickFormat. Raw }; using ( var image = new MagickImage ( stream, settings )) { }

Is it possible to load a file into a FILESTREAM?

After I select the file, "open" is populated with several items, including the path. Now I would like to load the file into a filestream (or something similar) to be sent via a webservice... is this possible? Yes, it is possible. Any other questions?

Why am I getting try load from stream I catch error?

If try load from stream I catch error. Sorry, something went wrong. ImageMagick will try to guess the file format because you are reading from a stream and guesses that your file is a TIFF file. When reading from file it sees the .arw extension and knows your file is a RAW file.

Is it possible to use stream with bitmapimage?

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: For instance when you have your photo stored as a byte [] array you can use the stream to convert it to image:


2 Answers

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.

like image 72
Johhny Bravo Avatar answered Oct 12 '22 23:10

Johhny Bravo


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?

like image 33
Daniel Krzyczkowski Avatar answered Oct 12 '22 22:10

Daniel Krzyczkowski