Why am I getting "System.Array" does not contain a definition for "AsBuffer" with this WinRT code?

According to this , the following code can be used to convert a byte array to a BitmapImage:

public static async Task<BitmapImage> ByteArrayToBitmapImage(this byte[] byteArray)
{
    if (byteArray != null)
    {
        using (var stream = new InMemoryRandomAccessStream())
        {
            await stream.WriteAsync(byteArray.AsBuffer());
            var image = new BitmapImage();
            stream.Seek(0);
            image.SetSource(stream);
            return image;
        }
    }
    return null;
}

      

However, I get: "System.Array" does not contain a definition for "AsBuffer" and there is no extension method "AsBuffer" that takes a first argument of type "System.Array" (are you missing a directive or assembly reference?) "

Is it that the purpose of "var stream" is too vague (implicit typing) and I need to specify a specific datatype for the "stream" var? Anything other than System.Array?

Maybe it is from " Windows Store Apps" - this is the key: Buffers / Byte Arrays-System.Runtime.InteropServices.WindowsRuntime. WindowsRuntimeBufferExtensions: Extension methods of this class provide ways to move between .NET byte arrays and the contents of the WinRT buffers that are displayed as implementations of IBuffer.

... but if so, I don't have enough information to know what to do with it. Instead of "TMI" it is "NEI" (not enough information).

+3


source to share


1 answer


The problem is that the compiler doesn't find the extension method AsBuffer()

. Make sure you have a namespace reference System.Runtime.InteropServices.WindowsRuntime

i.e.

using System.Runtime.InteropServices.WindowsRuntime;

      



You also need to add a link to the appropriate DLL if you haven't already:

Namespace: System.Runtime.InteropServices.WindowsRuntime

Assembly: System.Runtime.WindowsRuntime (in System.Runtime.WindowsRuntime.dll)

+8


source







All Articles