Xaml windows8中位图图像与字节数组的转换

Xaml windows8中位图图像与字节数组的转换,xaml,windows-8,bytearray,bitmapimage,writeablebitmap,Xaml,Windows 8,Bytearray,Bitmapimage,Writeablebitmap,我在下面编写代码,将图像转换为字节数组,稍后再转换回来,但它不起作用。有人能帮忙吗 FileOpenPicker picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.Thumbnail; picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; picker.FileTypeFilter.Add(".

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

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;
        }
    }
公共静态异步任务ImageToByteArrayAsync(存储文件)
{
使用(irandomaccesstream=await file.OpenReadAsync())
{                
使用(DataReader=newDataReader(stream.GetInputStreamAt(0)))
{
wait reader.LoadAsync((uint)stream.Size);
byte[]pixeByte=新字节[stream.Size];
reader.ReadBytes(pixeByte);
返回像素字节;
}
}
}
//将字节数组转换为位图图像
公共静态异步任务ByteArrayToImageAsync(字节[]pixeByte)
{
使用(InMemoryRandomAccessStream=新建InMemoryRandomAccessStream())
{
BitmapImage=新的BitmapImage();
wait stream.WriteAsync(pixeByte.AsBuffer());
stream.Seek(0);
image.SetSource(流);
返回图像;
}
}

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

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;
}
public异步任务ImageToBytes(irandomaccesstream sourceStream)
{
字节[]图像数组;
BitmapDecoder decoder=等待BitmapDecoder.CreateAsync(sourceStream);
var transform=new BitmapTransform{ScaledWidth=decoder.PixelWidth,ScaledHeight=decoder.PixelHeight};
PixelDataProvider pixelData=await decoder.GetPixelDataAsync(
BitmapPixelFormat.Rgba8,
BitmapAlphaMode。笔直,
使改变
ExifOrientationMode.REPECTEXIFORIENTION,
颜色管理模式(DoNotColorManage);
使用(var destinationStream=new InMemoryRandomAccessStream())
{
BitmapEncoder编码器=等待BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId,destinationStream);
编码器.SetPixelData(BitmapPixelFormat.Rgba8,BitmapAlphaMode.Premultiply,decoder.PixelWidth,
decoder.PixelHeight,96,96,pixelData.DetachPixelData();
等待编码器。FlushAsync();
BitmapDecoder outputDecoder=等待BitmapDecoder.CreateAsync(destinationStream);
等待destinationStream.FlushAsync();
imageArray=(等待outputDecoder.GetPixelDataAsync()).DetachPixelData();
}
返回图像阵列;
}

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

你说的“它不工作”是什么意思?testImage2不显示图像检查这个答案嗨@Ross,谢谢你的回答。我找到了另一种方法。谢谢