Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Windows Phone 8.1中,如何将图像从库上载到服务器。。。?_C#_Xaml_Windows Phone 8.1 - Fatal编程技术网

C# 在Windows Phone 8.1中,如何将图像从库上载到服务器。。。?

C# 在Windows Phone 8.1中,如何将图像从库上载到服务器。。。?,c#,xaml,windows-phone-8.1,C#,Xaml,Windows Phone 8.1,如何将图像从库上载到服务器 我正在使用FileOpenPicker在屏幕中选择图像显示BitmapImage显示。这在将图像上传到服务器的同时工作正常。我正在使用此代码使用FileOpenPicker将图像拾取到库中 private async void MenuFlyoutItem_Click(object sender, RoutedEventArgs e) { ImagePath = string.Empty; FileOp

如何将图像从库上载到服务器

我正在使用
FileOpenPicker
在屏幕中选择图像显示
BitmapImage
显示。这在将图像上传到服务器的同时工作正常。我正在使用此代码使用
FileOpenPicker
将图像拾取到库中

 private async void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
        {
            ImagePath = string.Empty;
            FileOpenPicker filePicker = new FileOpenPicker();
            filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            filePicker.ViewMode = PickerViewMode.Thumbnail;

            // Filter to include a sample subset of file types
            filePicker.FileTypeFilter.Clear();
            filePicker.FileTypeFilter.Add(".bmp");
            filePicker.FileTypeFilter.Add(".png");
            filePicker.FileTypeFilter.Add(".jpeg");
            filePicker.FileTypeFilter.Add(".jpg");

            filePicker.PickSingleFileAndContinue();
            view.Activated += viewActivatedddd;

        }



private async void viewActivatedddd(CoreApplicationView sender, IActivatedEventArgs args1)
        {
            FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;

            if (args != null)
            {
                if (args.Files.Count == 0) return;

                view.Activated -= viewActivatedddd;
                StorageFile storageFile = args.Files[0];
                var imageFile = args.Files[0] as StorageFile;

                var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
                var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
                await bitmapImage.SetSourceAsync(stream);

                var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
                displayImage.Source = bitmapImage;
                btnclose.Visibility = Visibility.Visible;



            }
        }`

当我在利用WebAPI时遇到这个问题时,下面是我的解决方案

将图像转换为内存流

客户:

 public async Task UploadImage(byte[] image, string url)
        {
            Stream stream = new System.IO.MemoryStream(image);
            HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream());

            Uri resourceAddress = null;
            Uri.TryCreate(url.Trim(), UriKind.Absolute, out resourceAddress);
            Windows.Web.Http.HttpRequestMessage request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Post, resourceAddress);
            request.Content = streamContent;

            var httpClient = new Windows.Web.Http.HttpClient();
            var cts = new CancellationTokenSource();
            Windows.Web.Http.HttpResponseMessage response = await httpClient.SendRequestAsync(request).AsTask(cts.Token);
        }
控制器:

public async Task<HttpResponseMessage> Post()
{
    Stream requestStream = await this.Request.Content.ReadAsStreamAsync();
    byte[] byteArray = null;
    using (MemoryStream ms = new MemoryStream())
    {
        await requestStream.CopyToAsync(ms);
        byteArray = ms.ToArray();
    }
    .
    .
    .
    return Request.CreateResponse(HttpStatusCode.OK);
}
public async Task Post()
{
Stream requestStream=等待这个.Request.Content.ReadAsStreamAsync();
字节[]byteArray=null;
使用(MemoryStream ms=new MemoryStream())
{
等待requestStream.CopyToAsync(毫秒);
byteArray=ToArray女士();
}
.
.
.
返回Request.CreateResponse(HttpStatusCode.OK);
}

谢谢您的回复……但我没有使用视图模型控制器的正常结构。我第一次使用windows phone 8.1。请将代码添加到我的上述方法中。您的控制器是WebAPI处理服务调用所需的。我使用的是visuval studio 2013 professional和windows phone 8.1 json web services。您能花半小时做WebAPI辅导吗?然后您可以利用我的服务器部分解决方案。@ScottNimrod您可以上传您是如何在WP8.1中将图像转换为字节数组的吗?