0

I am trying to get the raw image data from a stream and I am not sure where to go from here. I have a viewmodel and a page where I use the function where I select a picture from a gallery using XLABS.

My viewmodel:

public ImageSource ImageSource { get { return _ImageSource; } set { SetProperty (ref _ImageSource, value); } } private byte[] imageData; public byte[] ImageData { get { return imageData; } } private byte[] ReadStream(Stream input) { byte[] buffer = new byte[16*1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } return ms.ToArray(); } } public async Task SelectPicture() { Setup (); ImageSource = null; try { var mediaFile = await _Mediapicker.SelectPhotoAsync(new CameraMediaStorageOptions { DefaultCamera = CameraDevice.Front, MaxPixelDimension = 400 }); VideoInfo = mediaFile.Path; ImageSource = ImageSource.FromStream(() => mediaFile.Source); } catch (System.Exception ex) { Status = ex.Message; } } private static double ConvertBytesToMegabytes(long bytes) { double rtn_value = (bytes / 1024f) / 1024f; return rtn_value; } 

The page where I use the code:

MyViewModel photoGallery = null; photoGallery = new MyViewModel (); private async void btnPickPicture_Clicked (object sender, EventArgs e) { await photoGallery.SelectPicture (); imgPicked.Source = photoGallery.ImageSource; //imgPicked is my image x:name from XAML. } 

1 Answer 1

3

this will initialize the ImageData property you already have defined on your ViewModel

VideoInfo = mediaFile.Path; ImageSource = ImageSource.FromStream(() => mediaFile.Source); // add this line imageData = ReadStream(mediaFile.Source); 
Sign up to request clarification or add additional context in comments.

13 Comments

Error: Property or indexer "Project.MyViewModel.ImageData" cannot be assigned to (it is read-only)
typo, use lower case "imageData"
The code runs but when I use it in my btnPickPicture_Clicked and write it in the log I get this: imgPicked.Source = pG.ImageSource; System.Diagnostics.Debug.WriteLine (imgPicked.Source); In the log it says: "Xamarin.Forms.StreamImageSource"
imgPicked.Source is a stream - writing it to the console isn't particularly useful
Oh ok. How do write out the raw image data then? Is it the photoGallery.ImageSource where the raw data is?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.