19619

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:

<strong>Exception</strong>

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()"

<strong>Extra</strong>

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

Answer1:

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

Recommend

  • Putting text shadow on a UIButton text label
  • How to save a large fractal image with the least possible memory footprint?
  • OpenCV OpenNI calibrate kinect
  • How to separate filename from path? basename() versus preg_split() with array_pop()
  • How to extract a number from a string [duplicate]
  • How to use the resource module to measure the running time of a function?
  • Ionic storage “get” returns null only on the second call within method
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Google Places API - Find a company's CID and LRD
  • Why does it draw lines in the wrong place?
  • Cloud Code function running twice
  • what makes a request a new request in asp.net C#
  • Defined variables not working in javascript files when I use getScript
  • ThreadStatic in asynchronous ASP.NET Web API
  • Scipy Leastsq Optional Output Variable (Mesg)
  • Scala multiline string placeholder
  • Thread safety of a fluent like class using clone() and non final fields
  • Assign variable to the value in HTML
  • Keep this build forever option - Jenkins
  • Converting a WriteableBitmap image ToArray in UWP
  • How to define and use opencv mat of user type
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Why value captured by reference in lambda is broken? [duplicate]
  • Is there any way to access browser form field suggestions from JavaScript?
  • Resize panoramic image to fixed size
  • Volusion's generic SQL folder, functionality
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Updating server-side rendering client-side
  • javascript inside java/jsp code
  • htaccess rewriting URLs with multiple forward slashes
  • Hazelcast - OperationTimeoutException
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • How to format a variable of double type
  • Android Studio and gradle
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML