UWP-图片缓存本地之图片与byte数组之间的转换

1、提取image control的source

2、从image control中提取图片byte 

 var himage = (BitmapImage)this.Img.Source;

                RandomAccessStreamReference random = RandomAccessStreamReference.CreateFromUri(himage.UriSour‌​ce);
                IRandomAccessStreamWithContentType streamWithContent = await random.OpenReadAsync();
                byte[] buffer = new byte[streamWithContent.Size];
                await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);

3、将byte数据转成Image 对象

 BitmapImage image = new BitmapImage();
                using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
                {
                    await stream.WriteAsync(obj.ImgArray.AsBuffer());
                    stream.Seek(0);
                    await image.SetSourceAsync(stream);
                }


猜你喜欢

转载自blog.csdn.net/smj20170417/article/details/80484545