2

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.

JTIM
  • 2,774
  • 1
  • 34
  • 74

1 Answers1

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;
            }
        }
  • The image is not a file, but rather from a server or from another bitmapImage. Any changes you know how to convert that, to test your code? – JTIM Oct 02 '16 at 15:31
  • @JTIM By replacing the using with await file by: using (Stream stream = await (response.Content.ReadAsStreamAsync())) { using (IRandomAccessStream strm = stream.AsRandomAccessStream()) { ... – Jean-Claude Colette Oct 02 '16 at 16:04
  • It freezes when the line `bi.SetSource(strm)` is executed. Nothing happens after that? – JTIM Oct 02 '16 at 17:00
  • It should be `await bi.SetSourceAsync(strm);` instead, seems like it continues over that line at least. – JTIM Oct 02 '16 at 17:06
  • 1
    The image size is correct, but the pixel data seems to be entirely 0,0,0,255. – JTIM Oct 02 '16 at 18:13
  • 1
    I changed the code. Missing transform with the dimensions of the image! I deleted the useless BitmapImage. It works for me. – Jean-Claude Colette Oct 03 '16 at 00:13
  • Cool, I will try this as soon as possible. – JTIM Oct 03 '16 at 07:38
  • I know this is not allowed, but Thank you :) – JTIM Oct 16 '16 at 16:59