简体   繁体   中英

Converting a WriteableBitmap image ToArray in UWP

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.

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM