我正在尝试使用UWP上的C#将BitmapImage存储到文件系统 . 图像使用图形api从Facebook下载并作为BitmapImage返回 . 该部分工作,并检索图像(一旦我可以存储它,测试刚刚放在本地文件夹中的图片)我正在使用以下代码:
public static async Task<BitmapImage> GetProfilePicture(string userId){
BitmapImage profilePicture = new BitmapImage();
StorageFolder pictureFolder = await
ApplicationData.Current.LocalFolder.GetFolderAsync("ProfilePictures");
StorageFile pictureFile = await pictureFolder.GetFileAsync(userId + ".jpg");
IRandomAccessStream stream = await pictureFile.OpenAsync(FileAccessMode.Read);
profilePicture.SetSource(stream);
return profilePicture;
这也有效,所以我想要的只是做相反的事情 . 首选结果如下:
public static async void SaveBitmapToFile(BitmapImage image, userId){
StorageFolder pictureFolder = await
ApplicationData.Current.LocalFolder.CreateFolderAsync(
"ProfilePictures",CreationCollisionOption.OpenIfExists);
//save bitmap to pictureFolder with name userId.jpg
}
我一直在寻找解决方案,但我似乎无法找到任何UWP平台 . 如何将Bitmap保存到文件?如果更容易使用其他扩展名,则扩展名不必是.jpg .
1 回答
如果使用WriteableBitmap会更容易 . 例如,第一种方法是:
然后你可以这样做: