UWP 从文件 StorageFile 转 SoftwareBitmap 图片方法

打印 上一主题 下一主题

主题 849|帖子 849|积分 2547

本文告诉大家如何在 UWP 从 文件 StorageFile 转 SoftwareBitmap 图片的方法
使用以下三步即可从文件 StorageFile 转 SoftwareBitmap 图片
第一步是读取文件,获取可以随机访问的 IRandomAccessStream 对象。这个对象表示的是一个 Stream 且此 Stream 支持随机访问。随机访问是和顺序访问相对,指的是可以从 Stream 的任意地方开始读写,代码如下
  1.             using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read))
  2.             {
  3.             }
复制代码
第二步是通过 BitmapDecoder 的 CreateAsync 创建出解码器
  1.   BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
复制代码
最后是通过解码器获取 SoftwareBitmap 对象
  1. var softwareBitmap = await decoder.GetSoftwareBitmapAsync();
复制代码
我封装的代码如下
  1.         private static async Task<SoftwareBitmap> StorageFileToSoftwareBitmapAsync(StorageFile inputFile)
  2.         {
  3.             using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read))
  4.             {
  5.                 // Create the decoder from the stream
  6.                 BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
  7.                 // Get the SoftwareBitmap representation of the file
  8.                 var softwareBitmap = await decoder.GetSoftwareBitmapAsync();
  9.                 return softwareBitmap;
  10.             }
  11.         }
复制代码
本文代码放在githubgitee 欢迎访问
可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码
  1. git init
  2. git remote add origin https://gitee.com/lindexi/lindexi_gd.git
  3. git pull origin cca7a541ecffad71371ff89f17108d7d04a9a102
复制代码
以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源
  1. git remote remove origin
  2. git remote add origin https://github.com/lindexi/lindexi_gd.git
复制代码
获取代码之后,进入 WadeaherkeaLihanececeeneri 文件夹

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

温锦文欧普厨电及净水器总代理

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表