0

Using XLABS in my xamarin forms project and I try to reach the underlying data of the image but I am not sure how to get it with my current code. I have a viewmodel and a page where I use the function and the function itself works fine. I can pick an image and get it. But I want to get the path/filedata.

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;
    }

My page where I use it:

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.

    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
DiddanDo
  • 401
  • 1
  • 5
  • 15

1 Answers1

1

MediaFile has a Path property. You even refer to it in your ViewModel

VideoInfo = mediaFile.Path;
Jason
  • 86,222
  • 15
  • 131
  • 146
  • Ah ok. But how do I go from there? When I take out the Imagesource from the gallery now I do only get a "Xamarin.Forms.FileImageSource" and I guess I would need a png/jpg to be able to store it externally? – DiddanDo Feb 03 '16 at 12:36
  • Should I make a new thread with the title "How do I get the underlying data from the image I pick from a gallery?" – DiddanDo Feb 03 '16 at 17:07
  • The MediaFile has both a Path and a Source (stream) property. Either use a stream or a file IO operation to read the data. – Jason Feb 03 '16 at 17:22
  • Okay. Should I read the data in my viewmodel or in my page where I use the function? – DiddanDo Feb 03 '16 at 17:34
  • Am i not using the method u are explaining here "ImageSource = ImageSource.FromStream(() => mediaFile.Source);"? – DiddanDo Feb 03 '16 at 17:52
  • Yes, you are. If you want to read the raw image data from a stream, see: http://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream – Jason Feb 03 '16 at 17:56
  • I am using that code as well right? 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(); } } – DiddanDo Feb 03 '16 at 17:59
  • You have it in your code, but it doesn't appear that you are ever using ReadStream(). – Jason Feb 03 '16 at 18:01
  • private byte[] ReadStream(Stream input) – DiddanDo Feb 03 '16 at 18:08
  • That is the definition of your method. You never call it anywhere else in your code, so it is never executed. – Jason Feb 03 '16 at 18:13
  • Ahh okay. That might be the problem then. I will make a new thread with that question then. Thanks alot Jason. – DiddanDo Feb 03 '16 at 18:17