windows8中bitmapimage和byte数组之间的转换

时间:2012-12-24 06:32:01

标签: xaml windows-8 bytearray bitmapimage writeablebitmap

我编写下面的代码将图像转换为字节数组并稍后转换回来,但它不起作用。有人可以帮忙吗?

FileOpenPicker picker = new FileOpenPicker();
        picker.ViewMode = PickerViewMode.Thumbnail;
        picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        picker.FileTypeFilter.Add(".jpg");
        picker.FileTypeFilter.Add(".png");
        StorageFile file = await picker.PickSingleFileAsync();
        byte[] pixeByte;
        using (IRandomAccessStream stream = await file.OpenReadAsync())
        {
            WriteableBitmap image = new WriteableBitmap(400, 250);
            image.SetSource(stream);
            testImage.Source = image;
            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
            BitmapTransform transform = new BitmapTransform();
            transform.ScaledWidth = 400;
            transform.ScaledHeight = 250;
            PixelDataProvider pixeldata =await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage);
            pixeByte = pixeldata.DetachPixelData();
        }

        using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
        {
            BitmapImage image = new BitmapImage();
            await stream.WriteAsync(pixeByte.AsBuffer());
            stream.FlushAsync().AsTask().Wait();
            stream.Seek(0);
             image.SetSource(stream);
            testImage2.Source = image;
        }

解决:

public static async Task<byte[]> ImageToByteArrayAsync(StorageFile file)
    {
        using (IRandomAccessStream stream = await file.OpenReadAsync())
        {                
            using (DataReader reader = new DataReader(stream.GetInputStreamAt(0)))
            {
                await reader.LoadAsync((uint)stream.Size);
                byte[] pixeByte = new byte[stream.Size];
                reader.ReadBytes(pixeByte);
                return pixeByte;
            }
        }
    }

    // Convert a byte array to BitmapImage
    public static async Task<BitmapImage> ByteArrayToImageAsync(byte[] pixeByte)
    {
        using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
        {
            BitmapImage image = new BitmapImage();
            await stream.WriteAsync(pixeByte.AsBuffer());
            stream.Seek(0);
            image.SetSource(stream);
            return image;
        }
    }

1 个答案:

答案 0 :(得分:2)

我已将用于将流转换为字节数组的部分取出: -

public async Task<byte[]> ImageToBytes(IRandomAccessStream sourceStream)
{
    byte[] imageArray;

    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream);

    var transform = new BitmapTransform { ScaledWidth = decoder.PixelWidth, ScaledHeight = decoder.PixelHeight};
    PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
        BitmapPixelFormat.Rgba8,
        BitmapAlphaMode.Straight,
        transform,
        ExifOrientationMode.RespectExifOrientation,
        ColorManagementMode.DoNotColorManage);

    using (var destinationStream = new InMemoryRandomAccessStream())
    {
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, destinationStream);
        encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, decoder.PixelWidth,
                                decoder.PixelHeight, 96, 96, pixelData.DetachPixelData());
        await encoder.FlushAsync();

        BitmapDecoder outputDecoder = await BitmapDecoder.CreateAsync(destinationStream);
        await destinationStream.FlushAsync();
        imageArray = (await outputDecoder.GetPixelDataAsync()).DetachPixelData();
    }
    return imageArray;
}

尝试一下,看看它是否适合你。