I have a Stream received from a PhotoResult
of photoChooserTask_Completed(object sender, PhotoResult e)
event handler.
The e.ChosenPhoto
itself a Stream so I assign it to Stream stream
. And I converted it to byte[] array using the method below:
public static byte[] ReadImageFile2(Stream mystream)
{
// The mystream.length is still full here.
byte[] imageData = null;
using (BinaryReader br = new BinaryReader(mystream))
{
imageData = br.ReadBytes(Convert.ToInt32(mystream.Length));
}
// But imageData.length is 0
return imageData;
}
I don't know what is wrong with the BinaryReader, it returns imageData
with just 0 length. Tried to cast type as br.ReadBytes((int)mystream.Length)
but still doesn't work.
Also tried all of the answers in Creating a byte array from a stream but still not working. Maybe my e.ChosenPhoto
cannot be used as a normal Stream.
Thank you.
The readObject method is called by Java, when a stream of byte is deserialized to Java object. This time, output will be updated based on the current year. Conclusively serialization is the process of converting an object into stream of bytes. The converted byte stream can be used for multiple purposes.
There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.
The BigInteger class has a longValue() method to convert a byte array to a long value: long value = new BigInteger(bytes). longValue(); Java.
According to the docs, you may have to set the position of the stream to 0 before reading it:
using (BinaryReader br = new BinaryReader(mystream))
{
mystream.Position = 0;
imageData = br.ReadBytes(Convert.ToInt32(mystream.Length));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With