1

I am supposed to get an Image from a url and convert it into 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[].

Is this the correct way to approach? Any kind of help would be appreciated.

winwaed
  • 7,645
  • 6
  • 36
  • 81
Tinku Chacko
  • 520
  • 1
  • 6
  • 20

4 Answers4

0

I haven't tried this but from my initial thought you will need a Stream. For that you could use StreamImageSource

From there you can use the code from this.

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
  • public static byte[] ReadStreamIntoByte (Stream input) { using (MemoryStream ms = new MemoryStream()) { input.CopyTo(ms); return ms.ToArray(); } } does not work with StreamImageSource as input parameter. –  Aug 08 '16 at 14:16
0

Xamarin.Forms inside it uses IImageSourceHandler which has realizations like

  • ImageLoaderSourceHandler - for UriImageSource
  • StreamImagesourceHandler - for StreamImageSource
  • and so on

For reading data from UriImageSource You should use ImageLoaderSourceHandler which is platform class. 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. There are a lot of tutorials how to get byte[] from them.

igofed
  • 1,402
  • 1
  • 9
  • 15
0

FFImageLoading (https://github.com/molinch/FFImageLoading/) CachedImage has GetImageAsJPG and GetImageAsPNG methods. It's a direct Xamarin.Forms Image class replacement with a lot of additional features.

Here's the code used for image retrieving (it can be used in a Image class custom renderers):

Daniel Luberda
  • 7,374
  • 1
  • 32
  • 40
  • Is your provided solution a solution to the question? I also wonder if it is still in development, because I cant build the solution. "FFImageLoading.Transformations.Windows" is not compatible with uap10.0" or "wpa81" and so on. –  Aug 09 '16 at 10:17
0

It's posible to use WebClient to obtain byte array from a URL.

var webClient = new WebClient ();
byte[] byteImage = webClient.DownloadData (imageURL);
Jovel Barrera
  • 11
  • 1
  • 5