Xamarin将Byte[]形成为未显示的图像源



嗨,我有一个从Xamarin Essentials Media Picker保存的byte[],我想在我的XAML页面上显示图像,所以我只有一个带有StackLayout的空白XAML页面,在我的代码中我执行以下操作:

Stream stream = new MemoryStream(filebyte);
var imageSource = ImageSource.FromStream(() => stream);
Image test = new Image();
test.Source = imageSource;
test.WidthRequest = 50;
test.HeightRequest = 50;
ImageStack.Children.Add(test);

但是当页面加载时,什么都不存在。我错过了什么。我甚至在一个新的项目中使用了最新版本的Xamarin Forms和Xamarin Essential的尝试过这一点

imgByteArray是Byte[],使用以下代码通过隐藏代码添加图像。

var stream1 = new MemoryStream(imgByteArray);
image.Source = ImageSource.FromStream(() => stream1);

image.WidthRequest = 50;
image.HeightRequest = 50;
imagestack.Children.Add(image);

更新:

在窗体中添加一个图像,将构建操作设置为嵌入式资源,然后将此图像转换为字节[],使用此字节[]显示图像。我没有问题。

var image = new Xamarin.Forms.Image();
var assembly = this.GetType().GetTypeInfo().Assembly;
byte[] imgByteArray = null;
var s = assembly.GetManifestResourceStream("FormsSample.f19.png");

if (s != null)
{
var length = s.Length;
imgByteArray = new byte[length];
s.Read(imgByteArray, 0, (int)length);              
var stream1 = new MemoryStream(imgByteArray);
image.Source = ImageSource.FromStream(() => stream1);

image.WidthRequest = 50;
image.HeightRequest = 50;
imagestack.Children.Add(image);
}         

最新更新