【问题标题】:Converting a WriteableBitmap image ToArray in UWP在 UWP 中将 WriteableBitmap 图像转换为 ToArray
【发布时间】:2016-10-02 13:43:13
【问题描述】:

我正在访问我的 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.Marshal.CopyToManaged(IntPtr source, Object destination, Int32 startIndex, Int32 length)\r\n 在 System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.CopyTo(IBuffer source, UInt32 sourceIndex , Byte[] destination, Int32 destinationIndex, Int32 count)\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 at Stonegaard_endless_runner.MainPage.d__7.MoveNext()"

额外

我已检查我是否有线程访问图像。

【问题讨论】:

    标签: c# uwp writeablebitmap


    【解决方案1】:

    在某些情况下,您不能使用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;
                }
            }
    

    【讨论】:

    • 图像不是文件,而是来自服务器或另一个位图图像。您知道如何转换它以测试您的代码的任何更改吗?
    • @JTIM 通过将 using 替换为 await 文件: using (Stream stream = await (response.Content.ReadAsStreamAsync())) { using (IRandomAccessStream strm = stream.AsRandomAccessStream()) { .. .
    • bi.SetSource(strm) 行被执行时它会冻结。之后什么都没有发生?
    • 图像大小正确,但像素数据似乎完全是0,0,0,255。
    • 我更改了代码。缺少图像尺寸的变换!我删除了无用的BitmapImage。它对我有用。
    猜你喜欢
    • 2018-04-03
    • 2018-02-19
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多