本文告訴大家如何在 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;
}
}
本文代碼放在github 和 gitee 歡迎訪問
可以通過如下方式獲取本文的源代碼,先創建一個空檔案夾,接著使用命令列 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/

本作品采用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可,歡迎轉載、使用、重新發布,但務必保留文章署名[林德熙](http://blog.csdn.net/lindexi_gd)(包含鏈接:http://blog.csdn.net/lindexi_gd ),不得用于商業目的,基于本文修改后的作品務必以相同的許可發布,如有任何疑問,請與我[聯系](mailto:[email protected]),
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/508724.html
標籤:UWP
