Exemplo n.º 1
0
        private static async Task Read(AsyncFileStream stream, byte[] buffer, int offset, int count)
        {
            int remaining = count;

            while (remaining > 0)
            {
                int read = await stream.ReadAsync(buffer, offset, remaining);

                XAssert.AreNotEqual(0, read, "Unexpected EOF");
                remaining -= read;
                offset    += read;
            }
        }
        public async Task <BitmapImage> ReadImageAsync(string path)
        {
            logger.Info("Do ReadImageAsync:" + path);
            using (AsyncFileStream afs = new AsyncFileStream(path, FileMode.Open, FileAccess.Read))
            {
                byte[] buffer = new byte[afs.Length];
                await afs.ReadAsync(buffer, 0, (int)afs.Length); // 图片不能超过4G

                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = new MemoryStream(buffer);
                bitmapImage.EndInit();
                return(bitmapImage);
            }
        }