将图像资产保存到 UWP 中本地文件夹中的存储文件

Saving an image asset to a storage file in a local folder in UWP

我的 UWP 项目 ("Assets/Nophoto.jpg") 的资产文件夹中有一张图片,我想将其另存为应用程序本地文件夹中的 StorageFile

我见过在 StorageFile 中保存图像的唯一方法是使用 FilePicker

FileOpenPicker FilePicker = new FileOpenPicker();
            FilePicker.FileTypeFilter.Add(".jpeg");
            FilePicker.FileTypeFilter.Add(".png");
            FilePicker.FileTypeFilter.Add(".jpg");
            FilePicker.ViewMode = PickerViewMode.Thumbnail;
            FilePicker.CommitButtonText = "Picture";
            FilePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

            PictureFile = await FilePicker.PickSingleFileAsync();

我将如何在 StorageFile

中存储图像文件(.jpeg、.png、.bmp 等)

回答您的问题:

I have an image in the Assets folder of my UWP project ("Assets/Nophoto.jpg") that I would like to save as a StorageFile in the app's local folder.

你可以这样做,例如,通过 Uri 获取文件(注意它也可以 ):

StorageFile sourceFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Nophoto.jpg"));
await sourceFile.CopyAsync(ApplicationData.Current.LocalFolder);

一旦你有了 StorageFile(无论是否通过 picker/from 安装 folder/other 获得)然后你可以将它复制到其他 存储文件夹。如果您通过流或像素缓冲区获得图像,则可以创建一个文件并使用流写入该文件。