C#Windows 8 Store(Metro,WinRT)字节数组到BitmapImage

mit*_*nko 15 c# bytearray bitmapimage microsoft-metro windows-store-apps

我正在开发一个将过滤器应用于图像的Windows 8 Metro应用程序.我有一个网络版的应用程序,并希望移植它.但是我们都知道WinRT没有.NET提供的所有好东西:/

目前我在字节数组上应用过滤器,我想保持这种方式,因为它超快!因此,在过去的几天里,我一直在寻找将StorageFile转换为byte []然后将byte []转换为BitmapImage的方法.

到目前为止,我已经设法完成了第一个(StorageFile到byte []).我是这样做的:

public async Task<Byte[]> ImageFileToByteArray(StorageFile file)
    {
        IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
        PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
        return pixelData.DetachPixelData();
    }
Run Code Online (Sandbox Code Playgroud)

这段代码返回byte[]包含像素数据的BGRA.

这是棘手的部分.我无法成功将字节数组转换为BitmapImage.我搜遍了所有的地方,很多人建议使用WriteableBitmap,但这对我没什么好处.我还发现了一些应该起作用的代码片段......但它们没有.

我尝试过的解决方案之一是使用InMemoryRandomAccessStream,如下所示:

public async Task<BitmapImage> ByteArrayToBitmapImage(Byte[] pixels)
    {
        var stream = new InMemoryRandomAccessStream();
        await stream.WriteAsync(pixels.AsBuffer());
        stream.Seek(0);
        var image = new BitmapImage();
        await image.SetSourceAsync(stream);
        return image;
    }
Run Code Online (Sandbox Code Playgroud)

这个抛出以下异常:

mscorlib.dll中出现"System.Exception"类型的异常,但未在用户代码中处理

附加信息:找不到该组件.(来自HRESULT的异常:0x88982F50)

我尝试使用这一行代替:

PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
            BitmapPixelFormat.Bgra8, 
            BitmapAlphaMode.Ignore, 
            new BitmapTransform(),
            ExifOrientationMode.IgnoreExifOrientation, 
            ColorManagementMode.DoNotColorManage);
Run Code Online (Sandbox Code Playgroud)

但是,由于我一直得到这个例外,这对我没有好处.

我也试过这个:

var bitmapImage = new BitmapImage();
        var pixels = await ImageFileToByteArray(file);
        ImageSource imgSource;
        using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
        {
            using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
            {
                writer.WriteBytes(pixels);
                await writer.StoreAsync();
            }

            await bitmapImage.SetSourceAsync(ms);
            imgSource = bitmapImage;
        }
Run Code Online (Sandbox Code Playgroud)

并获得与第一段代码相同的异常.

我还尝试了其他几种方法,包括使用普通的Stream然后转换为IRandomAccessStream,但它们也不起作用.

以上所有代码对我来说都很好.所以我现在的猜测是问题在于byte[].我猜测pixelData里面的格式是无效的,所以我尝试将它改成RGBA,但这也无济于事.BitmapImage的PixelHeight和PixelWidth也是0.

mca*_*ral 12

这对我有用,

    private async Task<BitmapImage> ByteArrayToBitmapImage(byte[] byteArray)
    {
        var bitmapImage = new BitmapImage();

        var stream = new InMemoryRandomAccessStream();
        await stream.WriteAsync(byteArray.AsBuffer());
        stream.Seek(0);

        bitmapImage.SetSource(stream);
        return bitmapImage;
    }
Run Code Online (Sandbox Code Playgroud)