转换'Xamarin.Form.Image'类型'byte[]'



我应该从url中获取一个Image并将其转换为byte[]。以下是我实现的原因

var webImage = new Image { Aspect = Aspect.AspectFit };
webImage.Source = ImageSource.FromUri(new    Uri("http://xamarin.com/content/images/pages/forms/example-app.png"));

但是现在我无法将图像类型转换为CCD_ 2。

这是正确的方法吗?任何形式的帮助都将不胜感激。

我还没有尝试过,但从我最初的想法来看,你需要一个Stream。为此,您可以使用StreamImageSource

从那里你可以使用这个代码。

Xamarin.Form内部使用IImageSourceHandler,它具有类似的实现

  • ImageLoaderSourceHandler-用于UriImageSource
  • StreamImagesourceHandler-用于StreamImageSource
  • 等等

要从UriImageSource读取数据,您应该使用ImageLoaderSourceHandler,它是平台类。因此,在每个平台下使用类似的代码

var handler = new ImageLoaderSourceHandler();
var uiimage = await handler.LoadImageAsync(source, new CancellationToken(), UIScreen.MainScreenScale);

然后,您可以从iOS下的UIImage或Android下的Bitmap访问任何数据。有很多关于如何从中获取byte[]的教程。

FFImageLoading(https://github.com/molinch/FFImageLoading/)CCD_ 9有CCD_ 10和CCD_。它是一个直接的Xamarin。用许多附加功能形成了Image类的替换。

以下是用于图像检索的代码(可以在Image类自定义渲染器中使用):

  • Android
  • iOS
  • Windows

可以使用WebClient从URL获取字节数组。

var webClient = new WebClient ();
byte[] byteImage = webClient.DownloadData (imageURL);

最新更新