【问题标题】:Metro APP - BitmapImage to Byte[] or Download Image from Web and convert it to a Byte[] ArrayMetro APP - BitmapImage to Byte[] or Download Image from Web and convert it to a Byte[] Array
【发布时间】:2012-05-06 16:43:37
【问题描述】:

有没有办法将 BitmapImage (Windows.UI.Xaml.Media.BitmapImage) 转换为 Byte[] 数组?我没有尝试过工作...... 另一种可能的情况(如果 BitmapImage 无法转换为 Byte 数组)是从 web 下载图像,然后将其转换为数组...

但我不知道我该怎么做... 如果有人有想法,那就太好了。

当前尝试:

        HttpClient http = new HttpClient();
        Stream resp = await http.GetStreamAsync("http://localhost/img/test.jpg");

        var ras = new InMemoryRandomAccessStream();
        await resp.CopyToAsync(ras.AsStreamForWrite());

        BitmapImage bi = new BitmapImage();
        bi.SetSource(ras);


        byte[] pixeBuffer = null;

        using (MemoryStream ms = new MemoryStream())
        {
            int i = bi.PixelHeight;
            int i2 = bi.PixelWidth;
            WriteableBitmap wb = new WriteableBitmap(bi.PixelWidth, bi.PixelHeight);

            Stream s1 = wb.PixelBuffer.AsStream();
            s1.CopyTo(ms);

            pixeBuffer = ms.ToArray();
        }

但它不起作用... i & i2 始终设置为 0。所以 ras 无法正常工作...。这是怎么回事?

谢谢

【问题讨论】:

    标签: c# image stream windows-runtime winrt-xaml


    【解决方案1】:

    在您的代码中,您永远不会等待 BitmapImage 从 Web 加载图像,因此当您访问它们时它不知道它的 PixelWidth/PixelHeight。你可以等待它加载——例如通过调用“await bi.WaitForLoadedAsync()”来使用AsyncUI库。如果您想访问解码的像素,这对您并没有真正的帮助,因为 BitmapImage 不允许您访问像素,并且目前没有 API 可以将 BitmapImage 转换为 WriteableBitmap。

    您可以查看有关该主题的earlier question。您需要使用HttpClient.GetAsync() 之类的内容从网络获取图像文件,然后使用BitmapDecoder 之类的内容加载它。

    【讨论】:

      猜你喜欢
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 2022-12-02
      • 2022-12-02
      • 2012-10-15
      • 2022-12-02
      相关资源
      最近更新 更多