是否有将ImageSource转换为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

初始问题:我们想通过HTTP Post上传图片,这是我们尝试的方法:

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);

对于使用子句中必须包含的库,我们也遇到了困难.似乎using System.IO;可以工作,但是它不能让我们访问FileInfoFileStream之类的类.

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.

除了定制平台特定的转换器之外,还有谁能做到这一点? 可能是Xamarin.Forms.ImageSource函数toByte()?

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.

TargetImageSourceXamarin.Forms.ImageSource.

ImageSource TargetImageSource = null;

解决方案(Sten是正确的)

Solution (Sten was right)

ImageSource必须源自另一种类型才能存在,该先前的类型可以转换为byte[].在这种情况下,我使用 Xamarin.Forms.Labs 拍照,并返回一个MediaFile,其中FileStream可通过Source属性访问.

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());

功能:

public static byte[] ReadFully(Stream input)
{
    using (MemoryStream ms = new MemoryStream()){
        input.CopyTo(ms);
        return ms.ToArray();
    }
}

我认为您正在向后看.

ImageSource是一种为Xamarin.Forms.Image提供源图像以显示某些内容的方法.如果您已经在屏幕上显示了某些内容,您的Image视图将填充来自其他地方的数据,例如文件或资源,或者存储在内存中的数组中……否则,您首先会得到它. .您可以保留对它的引用并根据需要上载,而不必尝试从ImageSource取回该数据.

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.

伪代码:

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);
}

更新:

由于要拍照,因此可以将MediaFile.Source流复制到内存流中,然后可以将内存流的位置重置为指向流的开头,以便您可以再次读取并复制它到http正文.

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.

或者,您可以将MediaFile.Source存储到文件中,并使用ImageSource.FromFile将其加载到UI中,并且在必要时-您可以将文件的内容复制到http帖子主体中.

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.