簡體   English   中英

我想使用XLABS從Xamarin Forms中的流中獲取圖像數據

[英]I want to get the image data from a stream in Xamarin Forms using XLABS

我正在嘗試從流中獲取原始圖像數據,但不確定從何處去。 我有一個視圖模型和一個頁面,在其中使用了使用XLABS從圖庫中選擇圖片的功能。

我的視圖模型:

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

我使用代碼的頁面:

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.

}

這將初始化您已經在ViewModel上定義的ImageData屬性

VideoInfo = mediaFile.Path;
ImageSource = ImageSource.FromStream(() => mediaFile.Source);

// add this line
imageData = ReadStream(mediaFile.Source);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM