【问题标题】:Windows Phone 7.1: Split byte[] image and convert to BitmapImageWindows Phone 7.1:拆分 byte[] 图像并转换为 BitmapImage
【发布时间】:2012-01-07 04:08:42
【问题描述】:

编辑:对于那些需要对图像进行 blit(即将 WriteableBitmap 的一部分复制到另一个)的人,您可以使用 Buffer.BlockCopy 并使用 WriteableBitmaps 的 Pixels int[] 数组作为参数。

我之前问过这个问题:Image won't load completely on Windows Phone 7.5

我已经在这个问题上工作了几个小时,并且尝试了几件事。我不熟悉图像类型等,因此我可能缺少基本理论(例如我无法拆分 byte[] 图像并将它们转换为 BitmapImage)。

我想做的是:

  1. 使用 WebClient 从 Web 下载图像 (JPEG)。
  2. 使用 PictureDecoder.DecodeJpeg 从 WebClient 返回给我的流中解码 JPEG 并获取 WriteableBitmap 对象
  3. 将 WriteableBitmap.Pixels 数组从 int[] 转换为 byte[]
  4. 将 byte[] 数组拆分为多个部分,这样它们就不会超过 2000x2000 的大小限制
  5. 将每个部分转换为 BitmapImage,以便我可以在我的 WP7.1 Silverlight 应用程序中使用它们。

但我在 System.Windows.dll 的这些行中得到了一个带有未指定错误的 System.Exception:

 firstImg.SetSource(ms);

 newImg.SetSource(ms2);

顺便说一句,我正在下载的 jpeg 是一个有效的 jpeg,我可以显示它而无需尝试拆分它。

编辑:我下载的 Jpeg 宽度小于 2000,高度大于 2000。

这是我的代码:

private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        WriteableBitmap rawImg = PictureDecoder.DecodeJpeg(e.Result);
        byte[] arr;
        int height = rawImg.PixelHeight;
        int count = 0;

        if (height < 2000)
            images.Add(new MyImage(rawImg));
        else
        {
            arr = ConvertToByte(rawImg);
            MemoryStream ms = new MemoryStream();
            ms.Write(arr, 0, 4 * rawImg.PixelWidth * 2000);
            count++;
            BitmapImage firstImg = new BitmapImage();
            firstImg.SetSource(ms);
            images.Add(new MyImage(firstImg));

            while (height > 2000)
            {
                MemoryStream ms2 = new MemoryStream();
                ms2.Write(arr, count*2000, 4 * rawImg.PixelWidth * Math.Min(arr.Length - count*2000, 2000));
                count++;
                height -= 2000;
                BitmapImage newImg = new BitmapImage();
                newImg.SetSource(ms2);
                images.Add(new MyImage(newImg));
            }
        }     
    }

private byte[] ConvertToByte(WriteableBitmap wb)
    {
        int w = wb.PixelWidth;
        int h = wb.PixelHeight;
        int[] p = wb.Pixels;
        int len = p.Length;
        byte[] result = new byte[4 * w * h];

        for (int i = 0, j = 0; i < len; i++, j += 4)
        {
            int color = p[i];
            result[j + 0] = (byte)(color >> 24);
            result[j + 1] = (byte)(color >> 16);
            result[j + 2] = (byte)(color >> 8);
            result[j + 3] = (byte)(color);
        }

        return result;
    }

【问题讨论】:

    标签: c# silverlight windows-phone-7


    【解决方案1】:

    在你写信给MemoryStream之后,职位就晋级了。在设置源之前,请重置位置。

    ms.Position = 0;
    

    编辑 - 您可以使用 WriteableBitmapEx。这是一个非常快速的库,可以执行与 WriteableBitmaps 之间的字节转换。您还可以使用blitting 功能从复制较大图像的部分创建一个新的WriteableBitmap。

    【讨论】:

    • 我仍然遇到同样的异常。
    • 可以查看 WriteableBitmapEx 库。
    • 谢谢。 WriteableBitmapEx 可以将像素从一个 WriteableBitmap 复制到另一个。完全符合我的需求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    相关资源
    最近更新 更多