繁体   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