Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a cross-platform solution to ImageSource to byte[]?

I did researches and fell on this solution: http://forums.xamarin.com/discussion/22682/is-there-a-way-to-turn-an-imagesource-into-a-byte-array

Initial question: http://forums.xamarin.com/discussion/29569/is-there-a-cross-platform-solution-to-imagesource-to-byte#latest

We want to upload an image through a HTTP Post, here's what we tried:

HttpClient httpClient = new HttpClient ();
byte[] TargetImageByte = **TargetImageSource**; //How to convert it to a byte[]?
HttpContent httpContent = new ByteArrayContent (TargetImageByte);
httpClient.PostAsync ("https://api.magikweb.ca/debug/file.php", httpContent);

We also are having a hard time with the libraries we gotta include in the using clauses. It seems like using System.IO; works, but it doesn't give us access to classes like FileInfo or FileStream.

Anybody has any idea how this can be done aside from custom platform-specific converters? Possibly a Xamarin.Forms.ImageSource function toByte()?

Lemme know if you need more information.

TargetImageSource is a Xamarin.Forms.ImageSource.

ImageSource TargetImageSource = null;

Solution (Sten was right)

The ImageSource has to originate from another type to exist, that previous type can be converted to a byte[]. In this case, I use the Xamarin.Forms.Labs to take a picture and it returns a MediaFile in which a FileStream is accessible through the Source property.

//--Upload image
//Initialization
HttpClient httpClient = new HttpClient ();
MultipartFormDataContent formContent = new MultipartFormDataContent ();
//Convert the Stream into byte[]
byte[] TargetImageByte = ReadFully(mediaFile.Source);
HttpContent httpContent = new ByteArrayContent (TargetImageByte);
formContent.Add (httpContent, "image", "image.jpg");
//Send it!
await httpClient.PostAsync ("https://api.magikweb.ca/xxx.php", formContent);

App.RootPage.NavigateTo (new ClaimHistoryPage());

The function:

public static byte[] ReadFully(Stream input)
{
    using (MemoryStream ms = new MemoryStream()){
        input.CopyTo(ms);
        return ms.ToArray();
    }
}
like image 318
Vincent Poirier Avatar asked Dec 17 '14 18:12

Vincent Poirier


1 Answers

I think you're looking at it a bit backwards.

ImageSource is a way to provide a source image for Xamarin.Forms.Image to show some content. If you're already showing something on the screen your Image view was populated with data that came from elsewhere, such as a file or resource or stored in an array in memory... or however else you got that in the first place. Instead of trying to get that data back from ImageSource you can keep a reference to it and upload it as needed.

Maybe you can elaborate a bit on your particular need if you don't feel this solution applies to your case.

Pseudo code:

ShowImage(){
  ImageSource imageSource = ImageSource.FromFile("image.png"); // read an image file
  xf_Image.Source = imageSource; // show it in your UI
}

UploadImage(){
  byte[] data =  File.ReadAll("image.png");
  // rather than 
  // byte[] data = SomeMagicalMethod(xf_Image.Source);
  HttpClient.Post(url, data);
}

UPDATE:

Since you're taking a picture you can copy the MediaFile.Source stream into a memory stream, then you can reset the memory stream's position to point at the beginning of the stream so that you can read it once again and copy it to the http body.

Alternatively you can store the MediaFile.Source to a file and use ImageSource.FromFile to load it in the UI, and when necessary - you can copy the file's contents into an http post body.

like image 73
Sten Petrov Avatar answered Nov 03 '22 21:11

Sten Petrov