UWP 从文件 StorageFile 转 SoftwareBitmap 图片方法
本文告诉大家如何在 UWP 从 文件 StorageFile 转 SoftwareBitmap 图片的方法
使用以下三步即可从文件 StorageFile 转 SoftwareBitmap 图片
第一步是读取文件,获取可以随机访问的 IRandomAccessStream 对象。这个对象表示的是一个 Stream 且此 Stream 支持随机访问。随机访问是和顺序访问相对,指的是可以从 Stream 的任意地方开始读写,代码如下
using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read))
{
}
第二步是通过 BitmapDecoder 的 CreateAsync 创建出解码器
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
最后是通过解码器获取 SoftwareBitmap 对象
var softwareBitmap = await decoder.GetSoftwareBitmapAsync();
我封装的代码如下
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;
}
}
可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin cca7a541ecffad71371ff89f17108d7d04a9a102
以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
获取代码之后,进入 WadeaherkeaLihanececeeneri 文件夹
博客园博客只做备份,博客发布就不再更新,如果想看最新博客,请访问 https://blog.lindexi.com/
如图片看不见,请在浏览器开启不安全http内容兼容

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名[林德熙](https://www.cnblogs.com/lindexi)(包含链接:https://www.cnblogs.com/lindexi ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我[联系](mailto:lindexi_gd@163.com)。
分类:
UWP
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· c# 半导体/led行业 晶圆片WaferMap实现 map图实现入门篇