Converting a WriteableBitmap image ToArray in UWP

荒凉一梦 提交于 2020-01-04 14:03:56

问题


I am accessing an image from a blob storage on my azure service. I am returning the uri for the image and then uses HttpClient to try and download it. The uri is verified to be correct.

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

The image from the server is stored in the variable myOnlineImage. I then want to extract the pixel information using myOnlineImage.PixelBuffer.ToArray();. Is this because the image has not been downloaded correctly? Can anyone help me with options to solve this?

The exception that I am receiving is:

Exception

Message "Value cannot be null.\r\nParameter name: source"

StackTrace " at System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr source, Object destination, Int32 startIndex, Int32 length)\r\n at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.CopyTo(IBuffer source, UInt32 sourceIndex, Byte[] destination, Int32 destinationIndex, Int32 count)\r\n at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source, UInt32 sourceIndex, Int32 count)\r\n at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source)\r\n at Stonegaard_endless_runner.MainPage.d__7.MoveNext()"

Extra

I have checked that I have thread access to the image.


回答1:


In some case, you cannot use System.Runtime.InteropServices. The pointer myOnlineImage.PixelBuffer is null. You can use BitmapImage but not WriteableBitmap and convert to byte[] with BitmapDecoder, BitmapFrame, PixelDataProvider like the example of the 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;
            }
        }


来源:https://stackoverflow.com/questions/39817619/converting-a-writeablebitmap-image-toarray-in-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!