在win8中Image和Base64的之间的转换

image转换为Base64:

  //将图片转换成base64字符串
        public async static Task<String> convertToString(Image image)
        {
            //var storageFolder = KnownFolders.PicturesLibrary;
            ImageSource source = image.Source;
            string imagestr = (source as BitmapImage).UriSource.AbsolutePath.Substring(1);
            imagestr = imagestr.Replace("/", "\\");
            //BitmapImage bitamp = new BitmapImage();
            //bitamp = source as BitmapImage;
            var storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFile file = await storageFolder.GetFileAsync(imagestr);
            IRandomAccessStream filestream = await file.OpenAsync(FileAccessMode.Read);
            var reader = new DataReader(filestream.GetInputStreamAt(0));
            await reader.LoadAsync((uint)filestream.Size);
            byte[] pixels = new byte[filestream.Size];
            reader.ReadBytes(pixels);
            string strbase64 = Convert.ToBase64String(pixels);
            return strbase64;
        }

base64转换为图片

 public async static Task<Image> convertToImage(String strimage)
        {
            Image image = new Image();
            try
            {
                byte[] bitmapArray;
                bitmapArray = Convert.FromBase64String(strimage);
                MemoryStream ms = new MemoryStream(bitmapArray);
                InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
                //将randomAccessStream 转成 IOutputStream
                var outputstream = randomAccessStream.GetOutputStreamAt(0);
                //实例化一个DataWriter
                DataWriter datawriter = new DataWriter(outputstream);
                //将Byte数组数据写进OutputStream
                datawriter.WriteBytes(bitmapArray);
                //在缓冲区提交数据到一个存储区
                await datawriter.StoreAsync();

                //将InMemoryRandomAccessStream给位图
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(randomAccessStream);

                image.Source = bitmapImage;
            }
            catch 
            {

            }
            return image;
        }
不积跬步,无以至千里!
原文地址:https://www.cnblogs.com/xiaolifeidao/p/2870983.html