c# - 通过网络请求传输图像

标签 c# xamarin xamarin.forms

我正在处理 Xamarin.Forms PCL 项目,并希望传输用户通过网络请求选择的照片

我正在使用下面的代码,但不知道用什么作为图像值,我只是

Image image = *selected image*
var values = new Dictionary<string, string>
        {
            {"user_session", session},
            {"image", ??? },
            . . . 
        };

        var content = new FormUrlEncodedContent(values);
        var webResponse= await App.client.PostAsync(App.URL + "test.php", content);
        var response= await webResponse.Content.ReadAsStringAsync();

最佳答案

您可以将照片转换为字符串,然后将其作为 Post 方法的内容发送。

 MediaFile file; //add xam.plugin.media nuget package by jamesmontemagno

 var stream = file.GetStream();
            var bytes = new byte[stream.Length];
            await stream.ReadAsync(bytes, 0, (int)stream.Length);
            string content = System.Convert.ToBase64String(bytes);

您还可以使用 xam.plugin.media 包来选择/拍照。 阅读更多 Here .

关于c# - 通过网络请求传输图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49586012/

相关文章:

c# - 如何将 XSD.exe 与 attributeGroup ref 一起使用

c# - CS0117 C# 'Resource' 不包含 'Drawable' 的定义

mvvm - 接口(interface)绑定(bind)到没有 MVVM 包的 ViewModel

android - Xamarin Forms 主详细信息页面将返回按钮更改为汉堡菜单

c# - 更改WPF中的鼠标拖动光标

c# - MVC Owin Cookie 身份验证 - 覆盖 ReturnUrl 生成

ios - 如何在 uicollectionview 上添加阴影?

c# - 如何在 Xamarin 表单中正确缩放手机的条目和按钮

c# - "Use of unassigned local variable"接口(interface)错误

c# - 返回多种类型