ImageSource 到 byte[] 是否有跨平台解决方案? [英] Is there a cross-platform solution to ImageSource to byte[]?

查看:20
本文介绍了ImageSource 到 byte[] 是否有跨平台解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了研究,发现了这个解决方案:http://forums.xamarin.com/discussion/22682/is-there-a-way-to-turn-an-imagesource-into-a-byte-array

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://forums.xamarin.com/discussion/29569/is-there-a-cross-platform-solution-to-imagesource-to-byte#latest

我们想通过 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 子句中的库.看起来 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.

TargetImageSource 是一个 Xamarin.Forms.ImageSource.

ImageSource TargetImageSource = null;

解决方案(Sten 是对的)

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

推荐答案

我认为您的看法有点倒退.

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

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.

这篇关于ImageSource 到 byte[] 是否有跨平台解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆