• 在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;
            }
    不积跬步,无以至千里!
  • 相关阅读:
    build-your-own-react 注释版
    react hooks 理念之代数效应
    用Robot Framework+python来测试基于socket通讯的C/S系统(网络游戏)
    使用svndumpfilter exclude来清理svn库的废弃文件实现差别备份
    使用SonarQube+Eclipse来分析python代码
    是该写点什么了
    EventLoop
    Promise 学习
    ES6 模块化导入导出
    Vue实现对象列表根据某一字段排序
  • 原文地址:https://www.cnblogs.com/xiaolifeidao/p/2870983.html
Copyright © 2020-2023  润新知