I'm using Ascetic.Plugins.Cropper inside my Xamarin project.
In MainViewModel page,
public static byte[] imgbyte;
public MainViewModel(ICropper cropper)
{
CropCommand = new Command(async () =>
{
Stream cropped = await cropper.Crop();
imgbyte = GetImageStreamAsBytes(cropped);
ResultPhotoSource = ImageSource.FromStream(() => cropped);
});
}
// Converting cropped image stream to bytes for later use
public byte[] GetImageStreamAsBytes(Stream imgstream)
{
var buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = imgstream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
The CropCommand is binded to the Command property of a button.
So the image stream is succesfully converted into bytes but ResultPhotoSource is null.
In my crop.xaml page I've added an Image tag:
<Image x:Name="cropped_img" Source="{Binding ResultPhotoSource }"/>
But cropped_img displays nothing.
If I remove the line imgbyte = GetImageStreamAsBytes(cropped);, then ResultPhotoSource is binded to the Source of Image tag succesfully.
Why can't I access same stream more than once?
Help would be appreciated.