Hello,
Welcome to Microsoft Q&A!
How to change background image using c# UWP
The code you are using is correct. But there is an implicit restriction for the UserProfilePersonalizationSettings.TrySetWallpaperImageAsync() Method, this API could only use files from the application local folder. You could see a comment ins the sample code in this document: pass in a relative path to a file inside the local appdata folder. It means you can't directly use images picked from random folders. To solve this issue, what you need is just to copy the image file you picked to the local folder and then use the image file from the local folder as the parameter in TrySetWallpaperImageAsync()
.
Like this:
FileOpenPicker picker = new FileOpenPicker();
picker.FileTypeFilter.Add("*");
StorageFile file = await picker.PickSingleFileAsync();
// copy file to the local folder
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile newfile = await file.CopyAsync(folder,"test.png",NameCollisionOption.ReplaceExisting);
if (file != null)
{
if (UserProfilePersonalizationSettings.IsSupported())
{
UserProfilePersonalizationSettings profileSettings = UserProfilePersonalizationSettings.Current;
bool success = await profileSettings.TrySetWallpaperImageAsync(newfile);
}
}
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.