[英]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. 任何帮助将不胜感激。
Xamarin.Forms inside it uses IImageSourceHandler
which has realizations like 其中的Xamarin.Forms使用IImageSourceHandler
,其实现如下
ImageLoaderSourceHandler
- for UriImageSource ImageLoaderSourceHandler
用于UriImageSource StreamImagesourceHandler
- for StreamImageSource StreamImagesourceHandler
用于StreamImageSource 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/ ) CachedImage
有GetImageAsJPG
和GetImageAsPNG
方法。 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.