简体   繁体   English

转换“ Xamarin.Form.Image”类型为“ byte []”

[英]Convert 'Xamarin.Form.Image' type 'byte[]'

I am supposed to get an Image from a url and convert it into byte[] . 我应该从URL获取图像并将其转换为byte[] The following is what I achieved 以下是我实现的

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

but now I am unable to convert the Image type to byte[] . 但是现在我无法将Image类型转换为byte[]

Is this the correct way to approach? 这是正确的方法吗? Any kind of help would be appreciated. 任何帮助将不胜感激。

I haven't tried this but from my initial thought you will need a Stream. 我还没有尝试过,但是从我最初的想法来看,您将需要一个Stream。 For that you could use StreamImageSource 为此,您可以使用StreamImageSource

From there you can use the code from this . 从那里,您可以使用this中的代码。

Xamarin.Forms inside it uses IImageSourceHandler which has realizations like 其中的Xamarin.Forms使用IImageSourceHandler ,其实现如下

  • ImageLoaderSourceHandler - for UriImageSource ImageLoaderSourceHandler用于UriImageSource
  • StreamImagesourceHandler - for StreamImageSource StreamImagesourceHandler用于StreamImageSource
  • and so on 等等

For reading data from UriImageSource You should use ImageLoaderSourceHandler which is platform class. 要从UriImageSource读取数据,应使用平台类ImageLoaderSourceHandler So under each platform use code like 所以在每个平台下使用类似

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

Then you can reach any data from UIImage under iOS or from Bitmap under Android. 然后,您可以从iOS下的UIImage或Android下的Bitmap获取任何数据。 There are a lot of tutorials how to get byte[] from them. 有很多教程如何从中获取byte []。

FFImageLoading ( https://github.com/molinch/FFImageLoading/ ) CachedImage has GetImageAsJPG and GetImageAsPNG methods. FFImageLoading( https://github.com/molinch/FFImageLoading/CachedImageGetImageAsJPGGetImageAsPNG方法。 It's a direct Xamarin.Forms Image class replacement with a lot of additional features. 它是Xamarin.Forms Image类的直接替代品,具有许多其他功能。

Here's the code used for image retrieving (it can be used in a Image class custom renderers): 这是用于图像检索的代码(可以在Image类自定义渲染器中使用):

It's posible to use WebClient to obtain byte array from a URL. 可以使用WebClient从URL获取字节数组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM