博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UWP 使用Windows.Web.Http命名空间下的HttpClient使用post方法,上传图片服务器
阅读量:4512 次
发布时间:2019-06-08

本文共 6074 字,大约阅读时间需要 20 分钟。

1.从相册里面选取图片

1         ///  2         /// 1.1 从相册里面选取图片 3         ///  4         ///  5         ///  6         private async void btnPhoto_Click(object sender, RoutedEventArgs e) 7         { 8             //创建和自定义 FileOpenPicker (从本地获取一张图片)  9             FileOpenPicker picker = new FileOpenPicker();10             picker.ViewMode = PickerViewMode.Thumbnail; //可通过使用图片缩略图创建丰富的视觉显示,以显示文件选取器中的文件  11             picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;//对话框打开时的默认路径(图片库)12             //设置可选择的文件类型13             picker.FileTypeFilter.Add(".jpg");14             picker.FileTypeFilter.Add(".jpeg");15             picker.FileTypeFilter.Add(".png");16             picker.FileTypeFilter.Add(".gif");17             //选取单个文件  18             StorageFile file = await picker.PickSingleFileAsync();19             if (file != null)20             {21                 CutPicture(file);//裁剪图片22             }23         }

2.从相机里面选取图片

1         ///  2         /// 1.2 相机 3         ///  4         ///  5         ///  6         private async void btnCamera_Click(object sender, RoutedEventArgs e) 7         { 8             CameraCaptureUI captureUI = new CameraCaptureUI(); 9             captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;//注意:这里只设置了返回图片格式:Jpeg如果是:Png(手机可以显示,在核心后台图片不显示)和JpegXR(都不显示)会报错;;根据实际情况来10             captureUI.PhotoSettings.AllowCropping = false;11             StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);12             if (photo != null)13             {14                 CutPicture(photo);//裁剪图片15             }16         }

3.裁剪图片调用的公共方法CutPicture

1         ///  2         /// 1.4 裁剪图片 3         ///  4         ///  5         public async void CutPicture(StorageFile file) 6         { 7             #region 裁剪图片 8             var inputFile = SharedStorageAccessManager.AddFile(file);//  获取一个文件共享Token,使应用程序能够与另一个应用程序共享指定的文件。 9             var destination = await ApplicationData.Current.LocalFolder.CreateFileAsync("Cropped.jpg", CreationCollisionOption.ReplaceExisting);//在应用文件夹中建立文件用来存储裁剪后的图像 10             var destinationFile = SharedStorageAccessManager.AddFile(destination);11             var options = new LauncherOptions();12             options.TargetApplicationPackageFamilyName = "Microsoft.Windows.Photos_8wekyb3d8bbwe";//应用于启动文件或URI的目标包的包名称13             //待会要传入的参数 14             var parameters = new ValueSet();15             parameters.Add("InputToken", inputFile);                //输入文件 16             parameters.Add("DestinationToken", destinationFile);    //输出文件 17             parameters.Add("ShowCamera", false);                    //它允许我们显示一个按钮,以允许用户采取当场图象(但是好像并没有什么用) 18             parameters.Add("EllipticalCrop", true);                 //截图区域显示为圆(最后截出来还是方形) 19             parameters.Add("CropWidthPixals", 300);20             parameters.Add("CropHeightPixals", 300);21             //调用系统自带截图并返回结果 22             var result = await Launcher.LaunchUriForResultsAsync(new Uri("microsoft.windows.photos.crop:"), options, parameters);23             if (result.Status == LaunchUriStatus.Success && result.Result != null)24             {25                 //对裁剪后图像的下一步处理 26                 try27                 {28                     // 载入已保存的裁剪后图片 29                     var stream = await destination.OpenReadAsync();30                     var bitmap = new BitmapImage();31                     await bitmap.SetSourceAsync(stream);32                       // 显示裁剪过后的图片 33                     imglogo.ImageSource = bitmap;34                     //此方法是请求后台修改图片35                     SetHeadPicture(destination);36                     OutBorder.Visibility = Visibility.Collapsed;37                 }38                 catch (Exception ex)39                 {40                     System.Diagnostics.Debug.WriteLine(ex.Message + ex.StackTrace);41                 }42             }43             #endregion44         }

 4.使用Windows.Web.Http命名空间下的HttpClient使用post方法,上传图片服务器

参考:

我的做法是将需要上传的参数放在HttpMultipartFormDataContent中,然后再使用HttpClient的PostAsync进行提交

请求参数:

1        2        ///  3         /// 向服务器发送post请求(修改头像) 4         ///  5         /// 路径 6         /// 
7 public async static Task
SendPostRequest(string url) 8 { 9 try10 {11 Dictionary
dic = new Dictionary
();12 dic.Add("GWnumber", setHeadPicture.GWnumber);13 dic.Add("token", setHeadPicture.Token);14 dic.Add("file", setHeadPicture.File);//file值是StorageFile类型15 dic.Add("systemType", setHeadPicture.SystemType);16 17 HttpMultipartFormDataContent form = new HttpMultipartFormDataContent();18 foreach (KeyValuePair
item in dic)19 {20 if (item.Key == "file")21 {22 StorageFile file = item.Value as StorageFile;23 HttpStreamContent streamContent = new HttpStreamContent(await file.OpenReadAsync());24 form.Add(streamContent, item.Key, "file.jpg");//注意:这里的值是必须的,图片所以使用的是HttpStreamContent25 }26 else27 {28 form.Add(new HttpStringContent(item.Value + ""), item.Key);29 }30 }31 HttpClient httpClient = new HttpClient();32 HttpResponseMessage response = await httpClient.PostAsync(new Uri(url), form).AsTask();33 var contentType = response.Content.Headers.ContentType;34 if (string.IsNullOrEmpty(contentType.CharSet))35 {36 contentType.CharSet = "utf-8";37 }38 return await response.Content.ReadAsStringAsync();39 40 }41 catch (Exception ex)42 {43 throw ;44 }45 }

uwp小白,请多指教!!

 

转载于:https://www.cnblogs.com/qq-smile/p/7273467.html

你可能感兴趣的文章
filebeat
查看>>
如何在Bitmap中画图?(MFC)
查看>>
laravel 多检索条件列表查询
查看>>
mysql 行转列 和 列转行
查看>>
有关时延扩展的双语句子
查看>>
工作多年后积累的设计灵活,稳定,优秀WinForms应用程序的最佳实践 WinForms best practice...
查看>>
iOS开发——高级篇——iOS键盘的相关设置(UITextfield)
查看>>
JVMGC机制
查看>>
IAR for AVR 报array is too large错误 【已解决】
查看>>
老子《道德经》第六十二章
查看>>
Junit问题01 利用 @Autowired 注入失效问题
查看>>
连通块
查看>>
servlet.txt笔记
查看>>
jquery设置select选中
查看>>
今天说一下DML触发器的顺序
查看>>
Memcached学习(一)--网络模型
查看>>
FragmentTransaction add 和 replace 区别 转
查看>>
jQuery 效果方法
查看>>
STM32物联网通信WIFI
查看>>
java反射案例详解
查看>>