0

从图像创建字节数组的最佳方法是什么?我见过很多方法,但在 WinRT 中没有一个有效。

4

4 回答 4

2

这是一种方法 http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imaging.writeablebitmap.aspx

或者,如果您将图像保存在 FS 上,只需创建一个 StorageFile 并使用流获取 byte[]

于 2013-02-21T15:38:36.203 回答
2
static class ByteArrayConverter
    {
        public static async Task<byte[]> ToByteArrayAsync(StorageFile file)
        {
            using (IRandomAccessStream stream = await file.OpenReadAsync())
            {
                using (DataReader reader = new DataReader(stream.GetInputStreamAt(0)))
                {
                    await reader.LoadAsync((uint)stream.Size);
                    byte[] Bytes = new byte[stream.Size];
                    reader.ReadBytes(Bytes);
                    return Bytes;
                }
            }
        }
    }
于 2013-03-12T10:26:45.100 回答
1

神奇之处在于DataReader 类。例如 ...

var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Logo.png"));
var buf = await FileIO.ReadBufferAsync(file);
var bytes = new byte[buf.Length];
var dr = DataReader.FromBuffer(buf);
dr.ReadBytes(bytes);
于 2013-02-22T04:41:17.973 回答
0

我使用了Charles Petzold的方法:

        byte[] srcPixels;
        Uri uri = new Uri("http://www.skrenta.com/images/stackoverflow.jpg");
        RandomAccessStreamReference streamRef = RandomAccessStreamReference.CreateFromUri(uri);

        using (IRandomAccessStreamWithContentType fileStream = await streamRef.OpenReadAsync())
        {
            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
            BitmapFrame frame = await decoder.GetFrameAsync(0);

            PixelDataProvider pixelProvider = await frame.GetPixelDataAsync();
            srcPixels = pixelProvider.DetachPixelData();

        }
于 2013-02-22T16:09:25.020 回答