在UWP

时间:2016-10-02 13:43:13

标签: c# uwp writeablebitmap

我正从我的Azure服务上的blob存储中访问图像。我正在为图像返回uri,然后使用HttpClient尝试下载它。 uri已经过验证是正确的。

using (HttpClient client = new HttpClient())
{
    try
    {
         HttpResponseMessage response = await client.GetAsync(new Uri(((App)Application.Current).results.statsInformation.ImageBlob.ImageUri, UriKind.RelativeOrAbsolute));
         if (response != null && response.StatusCode == HttpStatusCode.OK)
         {
             using (var stream = await response.Content.ReadAsStreamAsync())
             {
                  using (var memStream = new MemoryStream())
                  {
                       await stream.CopyToAsync(memStream);
                       memStream.Position = 0;
                       memStream.Seek(0, SeekOrigin.Begin);
                       myOnlineImage.SetSource(memStream.AsRandomAccessStream());
                  }
              }
         }
    }
    catch (Exception)
    {
        throw;
    }
}

来自服务器的图像存储在变量myOnlineImage中。然后,我想使用myOnlineImage.PixelBuffer.ToArray();提取像素信息。这是因为图片没有正确下载吗?任何人都可以帮我解决这个问题吗?

我收到的例外是:

异常

  

消息“值不能为空。\ r \ nParameter name:source”

     

StackTrace“在System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr源,对象目标,Int32 startIndex,Int32长度)\ r \ n在System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.CopyTo(IBuffer source,UInt32 sourceIndex) System.Runtime.InteropServices.WindowsRuntime上的System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer源,UInt32源索引,Int32计数)\ r \ n中的,Byte []目标,Int32 destinationIndex,Int32计数)\ r \ n .WindowsRuntimeBufferExtensions.ToArray(IBuffer source)\ r \ n在Stonegaard_endless_runner.MainPage.d__7.MoveNext()“

附加

我已经检查过我有线程访问图像。

1 个答案:

答案 0 :(得分:1)

在某些情况下,您无法使用System.Runtime.InteropServices。 指针myOnlineImage.PixelBuffer为空。 您可以使用BitmapImage但不能使用WriteableBitmap并使用转换为byte[] BitmapDecoderBitmapFramePixelDataProvider就像msdn:

的例子一样
        byte[] image_array = null;
        int image_array_width = 0;
        int image_array_height = 0;

        using (HttpClient client = new HttpClient())
        {
            try
            {
                HttpResponseMessage response = await client.GetAsync(new Uri("http://www.example.com/logo.png", UriKind.RelativeOrAbsolute));
                if (response != null && response.StatusCode == HttpStatusCode.OK)
                {
                    using (Stream stream = await (response.Content.ReadAsStreamAsync()))
                    {
                        using (IRandomAccessStream strm = stream.AsRandomAccessStream())
                        {
                            strm.Seek(0);
                            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(strm);
                            BitmapFrame bitmapFrame = await decoder.GetFrameAsync(0);
                            // Get the pixels
                            var transform = new BitmapTransform { ScaledWidth = decoder.PixelWidth, ScaledHeight = decoder.PixelHeight };
                            PixelDataProvider dataProvider =
                            await bitmapFrame.GetPixelDataAsync(BitmapPixelFormat.Bgra8,
                                                                    BitmapAlphaMode.Straight,
                                                                    transform,
                                                                    ExifOrientationMode.RespectExifOrientation,
                                                                    ColorManagementMode.ColorManageToSRgb);

                            await strm.FlushAsync();
                            image_array = dataProvider.DetachPixelData();
                            image_array_width = (int)decoder.PixelWidth;
                            image_array_height = (int)decoder.PixelHeight;
                            }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }