前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UWP 从文件 StorageFile 转 SoftwareBitmap 图片方法

UWP 从文件 StorageFile 转 SoftwareBitmap 图片方法

作者头像
林德熙
发布2022-08-12 19:58:41
3240
发布2022-08-12 19:58:41
举报
文章被收录于专栏:林德熙的博客林德熙的博客

本文告诉大家如何在 UWP 从 文件 StorageFile 转 SoftwareBitmap 图片的方法

使用以下三步即可从文件 StorageFile 转 SoftwareBitmap 图片

第一步是读取文件,获取可以随机访问的 IRandomAccessStream 对象。这个对象表示的是一个 Stream 且此 Stream 支持随机访问。随机访问是和顺序访问相对,指的是可以从 Stream 的任意地方开始读写,代码如下

代码语言:javascript
复制
            using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read))
            {
            }

第二步是通过 BitmapDecoder 的 CreateAsync 创建出解码器

代码语言:javascript
复制
  BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

最后是通过解码器获取 SoftwareBitmap 对象

代码语言:javascript
复制
var softwareBitmap = await decoder.GetSoftwareBitmapAsync();

我封装的代码如下

代码语言:javascript
复制
        private static async Task<SoftwareBitmap> StorageFileToSoftwareBitmapAsync(StorageFile inputFile)
        {
            using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read))
            {
                // Create the decoder from the stream
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                // Get the SoftwareBitmap representation of the file
                var softwareBitmap = await decoder.GetSoftwareBitmapAsync();
                return softwareBitmap;
            }
        }

本文代码放在githubgitee 欢迎访问

可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

代码语言:javascript
复制
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin cca7a541ecffad71371ff89f17108d7d04a9a102

以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源

代码语言:javascript
复制
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git

获取代码之后,进入 WadeaherkeaLihanececeeneri 文件夹

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档