#UWP Tips: C# code to change #Windows 10 Start Screen image


Windows 10 SDK provides API, by which you can programmatically change the Start Screen background image for both Windows 10 PC and Windows 10 Mobile. The API is located under Windows.System.UserProfile namespace. Using the API you can build apps and provides user easy access to change the start screen.

 

In this post, we are going to discuss about the API and also going to build a small demo app. The code snippet has been shared at the end of the post.

 

#UWP Tips: C# code to change #Windows 10 Start Screen image (www.kunal-chowdhury.com)

 

Windows 10 SDK provides API class “UserProfilePersonalizationSettings” under the “Windows.System.UserProfile” namespace. This class is sealed and hence you cannot inherit it. The class provides three methods and one property to handle user profile personalization.

 

The asynchronous method “TrySetWallpaperImageAsync” provided by the API allows you to set the background image of the start screen. You have to pass the object of the image as a StorageFile and returns the success status as a boolean value.

 

Let’s discuss the implementation with a demo app. To do this, first create a universal project. Open the new project dialog, navigate to Templates –> Visual C# –> Windows –> Universal and select the “Blank App (Universal Windows)” project template. Give it a name and click OK.

 

How to create a Universal Windows Platform app (www.kunal-chowdhury.com)

 

Now you design your UI as per your requirement. Let’s assume, we have a button on the UI which when clicked will set the background image of the Windows 10 Start Screen. On click event of the button, call the API to change the image. Below is the sample code for you to use:

 

private static async Task ChangeStartScreenBackground()
{
    if (UserProfilePersonalizationSettings.IsSupported())
    {
        StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(ImagePath));
        UserProfilePersonalizationSettings settings = UserProfilePersonalizationSettings.Current;
        var isSuccess = await settings.TrySetWallpaperImageAsync(file);
    }
}

 

First you need to check whether the system supports user profile personalization. You can check it by calling the static method UserProfilePersonalizationSettings.IsSupported(), as shown in the above code snippet. Then get the StorageFile object of the image that you want to set. You can get the instance by calling StorageFile.GetFileFromApplicationUriAsync method with the URI of the image. You can also call StorageFile.GetFileFromPathAsync method to get the image file.

 

At the end call the TrySetWallpaperImageAsync method of the current UserProfilePersonalizationSettings class by passing the image file. When called, it will change the image background return true/false based on the success.

 

Though this post was small but I hope, it will help you to understand the API, implement the code in your app and allow your app to change the background image. This works in both Windows 10 PC and Windows 10 Mobile.

 

Have a question? Drop a line below and I will try to answer you as soon as possible. Don’t forget to subscribe to my blog’s RSS feed and email newsletter. If you are on Twitter, Facebook or Google+, do connect with me. Also, I have a plenty of article on Windows 10 Tips and Tricks which you can read here: http://win10tips.kunal-chowdhury.com

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

1 comments

  1. Could you please provide a link to the C# project/soln implementing the above. I tried to imitate your code but couldn't get it to run. It seemed to hang at the GetFileFromPathAsync method, which I'm using in place of the GetFileFromApplicationUriAsync method.

    ReplyDelete

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top