How to change background image using c# UWP

Pham Cuong 66 Reputation points
2022-01-07T02:37:57.947+00:00

I'm using this code to change t162985-photo-2022-01-07-09-36-44.jpghe background image, but it doesn't change the background image

Developer technologies Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-01-07T05:49:29.203+00:00

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.