Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在UWP中将可写位图图像转换为阵列_C#_Uwp_Writeablebitmap - Fatal编程技术网

C# 在UWP中将可写位图图像转换为阵列

C# 在UWP中将可写位图图像转换为阵列,c#,uwp,writeablebitmap,C#,Uwp,Writeablebitmap,我正在从azure服务上的blob存储访问映像。我返回图像的uri,然后使用HttpClient尝试下载它。验证uri是否正确 using (HttpClient client = new HttpClient()) { try { HttpResponseMessage response = await client.GetAsync(new Uri(((App)Application.Current).results.statsInformation.Ima

我正在从azure服务上的blob存储访问映像。我返回图像的
uri
,然后使用HttpClient尝试下载它。验证
uri
是否正确

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;
    }
}
来自服务器的图像存储在变量
myOnlineImage
中。然后我想使用
myOnlineImage.PixelBuffer.ToArray()提取像素信息。这是因为图像未正确下载吗?有人能帮我解决这个问题吗

我收到的例外情况是:

例外情况

消息“值不能为空。\r\n参数名称:源”

StackTrace”在System.Runtime.InteropServices.Marshall.CopyToManaged(IntPtr源、对象目标、Int32 startIndex、Int32长度)\r\n在System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.CopyTo(IBuffer源、UInt32源索引、字节[]目标、Int32目标索引、Int32计数)\r\n位于System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source,UInt32 sourceIndex,Int32 count)\r\n位于System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source)\r\n位于Stonegaard\u Ungulard\u runner.MainPage.d\u 7.MoveNext()

额外的


我已检查是否具有对映像的线程访问权限。

在某些情况下,您不能使用
System.Runtime.InteropServices
。 指针
myOnlineImage.PixelBuffer
为空。 您可以使用
BitmapImage
但不能使用
WriteableBitmap
并将其转换为
byte[]
BitmapDecoder
BitmapFrame
PixelDataProvider
如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;
            }
        }

图像不是文件,而是来自服务器或其他bitmapImage。您知道如何转换这些内容以测试代码吗?@JTIM将using替换为await文件,方法是:using(Stream-Stream=await(response.Content.ReadAsStreamAsync()){using(irandomaccesstream-Stream-Stream=Stream.AsRandomAccessStream()){……行
bi.SetSource(strm)时,它会冻结
已执行。之后什么也没有发生?图像大小正确,但像素数据似乎完全是0,0,0255。我更改了代码。图像的尺寸缺少转换!我删除了无用的BitmapImage。它对我有效。