簡體   English   中英

如何從 mediaPicker 選取的圖像中獲取 MAUI.Graphics.IImage object?

[英]How does one get the MAUI.Graphics.IImage object from image picked from mediaPicker?

我一直在嘗試從我使用Microsoft.Maui.Media.MediaPicker選擇的圖像中獲取Microsoft.Maui.Graphics.IImage object 。

我試着用

var file = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
            {
                Title = "Pick a photo"
            });

var memoryStream = new MemoryStream();
var filestream = await file.OpenReadAsync();
await filestream.CopyToAsync(memoryStream);

byte[] byteArray = memoryStream.ToArray();
Microsoft.Maui.Graphics.IImage image;
Microsoft.Maui.Graphics.IImage newImage;
image = Microsoft.Maui.Graphics.Platform.PlatformImage.FromStream(memoryStream);

if (image != null)
{
    newImage = image.Resize(720, 1280, ResizeMode.Stretch, true);
}

現在,執行會拋出錯誤“對象未設置為對象的實例”。 我調試並看到雖然照片被挑選並被加載到 Memory Stream,'image' 變量在被賦予該行中的值后不存儲數據:

image = Microsoft.Maui.Graphics.Platform.PlatformImage.FromStream(memoryStream);

條件:

if (image != null)

返回 true,但使用調整大小 function 會引發“對象未設置為對象的實例”錯誤。

你為什么不使用你的字節數組?

嘗試這樣的事情:

Stream stream = await photo.OpenReadAsync();

stream.Position = 0;
byte[] byteArray;

using (BinaryReader br = new BinaryReader(stream))
{
   byteArray = br.ReadBytes((int)stream.Length);
}

因此,使用LoadImageFromBytes Here the documentation

希望能幫助到你。

感謝您的回復。 目前,我已經使用平台特定代碼分別為每個平台使用 Android.Graphics.Bitmap class 和 iOS UIImage class。 這使我能夠將圖像存儲在特定於平台的 class 對象中,而不是尋找多平台 class 來存儲圖像並調整其大小。

暫無
暫無

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

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